我编写了一个脚本mail.php来处理表单数据的验证并通过电子邮件发送表单数据。我一直在通过输入已知的无效数据来测试此脚本,但是该脚本不会回显错误消息,而只是发送数据。
这是html格式:
<form method="post" action="mail.php" class="col-12" id="form-book">
<div class="row">
<div class=" form-group col-12">
<p>Fill out the form below to tell me about your problem. <strong>Any problems to do with technology, all the solutions.</strong> Just tell me what you can, however the more info you give the better.</p>
</div>
<div class="form-group col-6">
<label></label>
<input id="name" name="name" placeholder="Name" type="text" required="required" class="form-control here bottom"> <span class="error"> <?php echo $nameErr; echo $nameErrEmpty;?></span>
</div>
<div class="form-group col-6">
<label></label>
<input id="phone" name="phone" placeholder="Phone#" type="text" required="required" class="form-control here bottom"> <span class="error"> <?php echo $phoneErr; echo$phoneErrEmpty;?></span>
</div>
<div class="form-group col-12">
<label></label>
<input id="email" name="email" placeholder="Email (Optional)" type="text" class="form-control here bottom"> <span class="error"> <?php echo $emailErr; echo $emailErrEmpty;?></span>
</div>
<div class="form-group col-6">
<input data-provide="datepicker" id="date" name="date" placeholder="Pick a date" required="required" class="form-control here bottom"> <span class="error"> <?php echo $dateErr; echo $dateErrEmpty;?></span>
</div>
<div class="form-group col-6">
<input type="text" id="time" name="time" placeholder="Choose the best time" required="required" class="form-control here bottom timepicker"> <span class="error"> <?php echo $timeErr; echo $timeErrEmpty;?></span>
</div>
<div class="form-group col-12">
<label></label>
<textarea id="message" name="message" cols="40" rows="5" class="form-control" required="required" aria-describedby="messageHelpBlock"></textarea> <span class="error"> <?php echo $messageErr; echo $messageErrEmpty;?></span>
<span id="messageHelpBlock" class="form-text text-muted">Tell me a little about whats going on or what you need help with.</span>
</div>
<div class="form-group col-12">
<button name="submit" type="submit" class="btn btn-primary">Send your message</button>
</div>
</div>
<!--row-->
</form>
这是mail.php:
<?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完全陌生,因此将不胜感激。谢谢!