我有点不知道如何正确验证此表格。实际上,它可以工作,但是当我输入正确/错误的内容时,然后按提交按钮,文本不会保存在输入字段中,这让我再次输入所有内容。我是 php 的菜鸟,这里有一些很棒的人帮助我在 PHP 中验证表单。另外,我想知道,是否可以同时使用 JS 和 php 来验证表单?如果用户禁用了 js,我将在客户端使用验证插件并在服务器上使用 php。如果您不介意请检查表单是否可以。顺便说一句,请禁用 js 以在 php 检查表单时查看问题。 https://www.dropbox.com/s/vka7cy70xv4zryf/formtest.zip 谢谢你的时间!
<title>FormTest</title>
<link rel="stylesheet" type="text/css" href="css.css"/>
<style type="text/css">
.formerrors li{color:yellow;
font-size:14px;
font-weight:lighter;
font-family:Tahoma, Geneva, sans-serif;}
</style>
</head>
<body>
<div id="contact">
<h1 class="heading1"> Contact:</h1>
<form name="contact" action="" method="post">
<label for="YourName">Your Name:</label>
<input type="text" name="name" class="required" />
<label for="YourEmail">Your Email:</label>
<input type="text" name="email" class="required"/>
<label for="Subject">Subject:</label>
<input type="text" name="subject" class="required" />
<label for="YourMessage">Your Message:</label>
<textarea name="message" class="required"></textarea>
<p class="c3">10 + 5 =<input type="text" name="answerbox" id="answerbox" /></p>
<fieldset>
<input type="submit" name="submit" id="submit" value="Send" class="required"/>
<input type="reset" id="reset" value="Reset"/>
</fieldset>
<?php
if(isset($_POST['submit'])){
$name = trim($_POST["name"]);
$email = trim($_POST["email"]);
$subject = trim($_POST["subject"]);
$message = trim($_POST["message"]);
$answerbox = trim($_POST["answerbox"]);
if(empty($_POST['name'])){
echo "<div class='formerrors'><li>Please type your name.</li></div>";
} else {
if (ctype_alpha($name) === false) {
echo "<div class='formerrors'><li>Your name only contain letters!</li></div>";
}
}
if(empty($_POST['email'])){
echo "<div class='formerrors'><li>You've forgot to type your email address.</li></div>";
} else{
if(filter_var($email, FILTER_VALIDATE_EMAIL) === false){
echo "<div class='formerrors'><li>Your email is not valid, please check.</li></div>";
}
}
if(empty($_POST['subject'])){
echo "<div class='formerrors'><li>Please type a subject.</li></div>";
}
if(empty($_POST['message'])){
echo "<div class='formerrors'><li>You've forgot to type your message.</li></div>";
}
if(empty($_POST['answerbox'])){
echo "<div class='formerrors'><li>Please answer the math question.</li></div>";
} else
if($answerbox != 15){
echo "<div class='formerrors'><li>Answer is not correct.</li></div>";
}
else{
$headers = 'From: '.$email. "\r\n" .
'Reply-To: '.$email . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail('me@mymail.me',$subject,$message,$headers);
print "Your message was sent successfully";
}
}
?>
</form>
</div><!--contact-->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript" src="jquery-1.8.3.min.js"></script>
<script type="text/javascript" src="jquery.validate.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
jQuery.validator.addMethod("lettersonly", function(value, element)
{return this.optional(element) || /^[a-z]+$/i.test(value);
}, "Letters only please.");
$("form").validate({
rules:{
name: {required: true,
maxlength:30,
lettersonly: true,},
email: {required: true,
email: true,
minlength: 5,
maxlength:40},
answerbox: {required: true,
max: 15,
min: 15},
},
});
});
</script>
</body>
</html>