我有一个表单,它接受数据,我使用php将其发送到我的电子邮件,一旦用户填写了所有必填字段。如果一个字段为空,我会收到一条消息,例如。 "Email is required"
但电子邮件仍然发送。我不知道什么问题是什么想法? Idont要发送电子邮件,如果任何字段为空,我也不想刷新页面每次提交被点击时,我想,而不是只显示"Required message".
PHP - 表单字段验证
<?php
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
$nameErr = $lastNameErr = $emailErr = $ironingErr = $descriptionErr = $RoomErr = "";
$first_name = $last_name = $email = $ironing = $description = $Rooms ="";
if(isset($_POST['submit'])){
$from = $_POST['email']; // this is the sender's Email address
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$ironing = $_POST['ironing'];
$Rooms = $_POST['Rooms'];
$Description = $_POST['description'];
if (empty($_POST["first_name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["first_name"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "Only letters and white space allowed";
}
}
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);
// check if e-mail address is well-formed
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
}
if (empty($_POST["description"])) {
$descriptionErr = "Description is required";
} else {
$description = test_input($_POST["description"]);
}
if (empty($_POST["Rooms"])) {
$RoomErr = "Room number is Required";
} else {
$Rooms = test_input($_POST["Rooms"]);
}
if (empty($_POST["ironing"])) {
$ironingErr = "Ironing is Required";
} else {
$ironing = test_input($_POST["ironing"]);
}
$to = "[email protected]"; // this is your Email address
$subject = "Order Sumbittion";
$subject2 = "Copy of your form submission";
$message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['description']. "\n\n" . "Number of Rooms: ". "\n\n" . $_POST['Rooms'] ."Ironing: " . $_POST['ironing'];
$message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['description']. "\n\n" . "Number of Rooms: " . "Number of Rooms: " . $_POST['Rooms'] ."Ironing: ". $_POST['ironing'];
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers);
mail($from,$subject2,$message2,$headers2);
// sends a copy of the message to the sender
echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
header("Location: index.php");
}
// You can also use header('Location: thank_you.php'); to redirect to another page.
}
?>
<p><span class="error">* required field.</span></p>
<div class="col-md-9">
<form action="" method="post">
First Name: <input type="text" name="first_name">
<span class="error">* <?php echo $nameErr;?></span><br>
<br>
Last Name: <input type="text" name="last_name">
<span class="error">* <?php echo $lastNameErr;?></span><br>
Email:
<br>
<input type="text" name="email">
<span class="error">* <?php echo $emailErr;?></span>
<br>
Ironing?<br>
<input type="radio" name="ironing" <?php if (isset($ironing) && $ironing=="Yes") echo "checked";?> value="Yes">Yes
<input type="radio" name="ironing" <?php if (isset($ironing) && $ironing=="No") echo "checked";?> value="No">No
<span class="error">* <?php echo $ironingErr;?></span>
<br>
Number Of Rooms:
<br>
<input type="text" name="Rooms">
<span class="error">* <?php echo $RoomErr;?></span>
<br>
Description of the House:
<br>
<textarea name="description" rows="10" cols="70"></textarea>
<span class="error">* <?php echo $descriptionErr;?></span>
<br>
<input type="submit" name="submit" value="Submit">
</form>
我建议在提交表单前在前端使用[Parsley javascript validation](http://parsleyjs.org/)。将是更好的用户体验,意味着您可以避免不必要地刷新页面。 – stevenkellow
我想避免尽可能多的JavaScript,而不是它的粉丝。 –
一些明智的代码缩进将是一个好主意。它可以帮助我们阅读代码,更重要的是,它可以帮助您**调试您的代码** [快速浏览编码标准](http://www.php-fig.org/psr/psr-2/ )为了您自己的利益。您可能会被要求在几周/几个月内修改此代码 ,最后您会感谢我。 – RiggsFolly