我已经在这个表单验证工作了一整天,我就快完成了,但它还不是很正确。我被告知在w3schools上使用这个例子,我使用了,但它不起作用。我花了一段时间来处理它,现在它确实有效了,但是有一些事情仍然是一团糟。如果有1个以上的错误,它不会显示出来。只会显示最后一个发生的事件。其次,我希望它通过页面上的输入显示错误。目前,它转到空白页面,只会回显错误。我尝试将它们包含在表单的html中,但它不起作用。我使用的例子是:
https://www.w3schools.com/php/showphp.asp?filename=demo_form_validation_complete
我的代码如下
html:
<form class="form inline d-flex justify-content-center" action="contact.php" method="POST" role="form">
<br style="clear:both">
<div class="col-sm-6">
<div class="form-group">
<input type="text" class="control" id="first_name" name="first_name" placeholder="First Name" required>
<span class="error">* <?php echo $nameErr;?></span>
</div>
<div class="form-group">
<input type="text" class="control" id="email" name="email" placeholder="Email" required>
<span class="error">* <?php echo $emailErr;?></span>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<input type="text" class="control" id="last_name" name="last_name" placeholder="Last Name" required>
<span class="error">* <?php echo $nameErr;?></span>
</div>
<div class="form-group">
<input type="text" class="control" id="phone" name="phone" placeholder="Phone" required>
<span class="error">* <?php echo $phoneErr;?></span>
</div>
</div>
<div class="col-sm-12">
<div class="form-group">
<textarea name="message" class="control" id="message" placeholder="What's on your mind?" rows="3"></textarea>
</div>
<button type="submit" id="submit" name="submit" class="btn btn-primary">SEND</button>
</form>
php:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
if (empty($first_name)) {
$nameErr = "First name is required";
} else {
$first_name = check_input($first_name);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$first_name)) {
$error = "Only letters and white space allowed";
} else {
$pass += 1;
}
}
if (empty($last_name)) {
$nameErr = "Last name is required";
} else {
$last_name = check_input($last_name);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$last_name)) {
$error = "Only letters and white space allowed";
}else {
$pass += 1;
}
}
if (empty($email)) {
$emailErr = "Email is required";
} else {
$email = check_input($email);
// check if e-mail address is well-formed
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$error = "Invalid email format";
}else {
$pass += 1;
}
}
if (empty($phone)) {
$phoneErr = "Phone number is required";
} else {
$phone = check_input($phone);
if (!preg_match("'^(([\+]([\d]{2,}))([0-9\.\-\/\s]{5,})|([0-9\.\-\/\s]{5,}))*$'",$phone)) {
$error = "Invalid Phone Number";
}else {
$pass += 1;
}
}
if (empty($message)) {
$error = "Message cannot be blank";
} else {
$message = check_input($message);
$pass += 1;
}
}
$email_from =' Client, llc';
$email_subject = 'New Message From A Guest';
$email_body = "Name: $first_name $last_name\n".
"Email: $email\n".
"Phone: $phone\n".
"Message: $message.\n";
$to ="me@work.com";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $email \r\n";
if (!isset($error) && $pass == '5'){
mail($to,$email_subject,$email_body,$headers);
header("location: thanks.html");
} else {
echo $error;
}
// purge the session after it is displayed
unset($error);
function check_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
我认为电话检查也不能正常工作,但我可以尝试不同的preg_match。我只需要让它正常工作。
https://stackoverflow.com/questions/51144937
复制相似问题