我有HTML表单,我与PHP一起使用。就在发送电子邮件之前,我希望PHP验证表单。这是我的PHP代码:
<?php
$name = $_POST['yourName'];
$email = $_POST['yourMail'];
$message = $_POST['yourMessage'];
$to = "blablabla@gmail.com";
$subject = "New mail from bla-bla.org";
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_SERVER['REQUEST_METHOD']== "POST") {
$valid = true;
if (empty($_POST["yourName"])) {
$firstNameErr = "First name is required";
$valid = false; //false
}
else {
$firstName = test_input($_POST["yourName"]);
}
if (empty($_POST["yourMail"])) {
$lastNameErr = "Last name is required";
$valid = false;
}
else {
$lastName = test_input($_POST["yourMail"]);
}
if (mail($to, $subject, $body)) {
header("Location: http://www.blabla.org/contact.html");
}
?>
点击Send
按钮后,我在浏览器中显示此行:
解析错误:语法错误,第32行/home/website/public_html/acknowledge.php中的文件意外结束
我一遍又一遍地查看我的代码,似乎无法找到任何错误。任何可能暗示出错的建议或新眼睛?
编辑:由于缺少}
,解析错误已解决。但是,如果没有PHP的实际验证,仍会发送空电子邮件。