-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphpsubmit.php
More file actions
52 lines (45 loc) · 1.31 KB
/
phpsubmit.php
File metadata and controls
52 lines (45 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
if ($_SERVER["REQUEST_METHOD"] === "POST") {
$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];
// Validate form data
$errors = [];
if (empty($name)) {
$errors[] = "Name is required";
}
if (empty($email)) {
$errors[] = "Email is required";
} elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors[] = "Invalid email format";
}
if (empty($message)) {
$errors[] = "Message is required";
}
// If there are errors, display them to the user
if (!empty($errors)) {
echo "<h2>Error:</h2>";
echo "<ul>";
foreach ($errors as $error) {
echo "<li>$error</li>";
}
echo "</ul>";
exit;
}
// Process the form submission
// Example: Send an email
$to = "youremail@example.com";
$subject = "New Contact Form Submission";
$message = "Name: $name\nEmail: $email\nMessage: $message";
$headers = "From: $email";
if (mail($to, $subject, $message, $headers)) {
// Success! Redirect to a thank you page
header("Location: thank-you.html");
exit;
} else {
// Error occurred while sending email
echo "An error occurred while sending your message. Please try again later.";
exit;
}
}
?>