-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm Handler with Validation.php
52 lines (46 loc) · 1.48 KB
/
Form Handler with Validation.php
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
$name = $email = $message = "";
$nameErr = $emailErr = $messageErr = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Name validation
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);
if (!preg_match("/^[a-zA-Z ]*$/", $name)) {
$nameErr = "Only letters and spaces are allowed";
}
}
// Email validation
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
}
// Message validation
if (empty($_POST["message"])) {
$messageErr = "Message is required";
} else {
$message = test_input($_POST["message"]);
}
// If no errors, send the email
if (empty($nameErr) && empty($emailErr) && empty($messageErr)) {
$to = "your-email@example.com"; // Replace with your email
$subject = "Contact Form Submission from $name";
$body = "Name: $name\nEmail: $email\nMessage: $message";
$headers = "From: $email";
if (mail($to, $subject, $body, $headers)) {
echo "Thank you for your message!";
} else {
echo "There was an error sending your message.";
}
}
}
// Function to clean input data
function test_input($data) {
return htmlspecialchars(stripslashes(trim($data)));
}
?>