我已经编写了一个脚本mail.php来处理表单数据的验证并通过电子邮件发送表单数据。我一直在通过输入已知的无效数据来测试这个脚本,但是这个脚本并不回显错误消息,只是发送数据。
以下是HTML表单:
Fill out the form below to tell me about your problem. Any problems to do with technology, all the solutions. Just tell me what you can, however the more info you give the better.
Tell me a little about whats going on or what you need help with.
Send your message
这里是mail.php:
error_reporting(-1);
ini_set('display_errors', 'On');
set_error_handler("var_dump");
date_default_timezone_set("Pacific/Honolulu");
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$date = $_POST['date'];
$time = $_POST['time'];
$message = $_POST['message'];
$subject = "Appointment Booked";
$mailTo = "itguy@johnpuaoi.com";
$email = filter_var($email, FILTER_SANITIZE_EMAIL);
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
if (empty($name)) {
$nameErrEmpty = "Your name is required";
} elseif (is_int($name)) {
$nameErr = "That is not a valid name";
} else {
$nameTested = test_input($name);
}
if (empty($phone)) {
$phoneErrEmpty = "Your phone number is required";
} elseif (is_int($name)) {
$phoneTested = test_input($phone);
} else {
$phoneErr = "Phone Number needs to be in this format e.x 1234567890";
}
if (empty($email)) {
$emailErrEmpty = "Your name is required";
} elseif (filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "That is not a valid email";
} else {
$emailTested = test_input($name);
}
if (empty($message)) {
$messageErrEmpty ="You must tell me about your problem";
} else {
$messageTested = test_input($message);
}
$dateTested = test_input($date);
$timeTested = test_input($time);
$headers = "From: ".$emailTested;
$txt = "An appointment was booked by ".$nameTested.".\n\n".$dateTested." @".$timeTested.".\n\n".$messageTested;
mail($mailTo, $subject, $txt, $headers);
header("Location: index.php?mailsend");
}
?>
我在堆栈中搜索过类似的问题,但没有找到。我完全不懂php,所以任何帮助都将不胜感激。谢谢!