我对如何正确验证此表单感到困惑。实际上,它的工作方式,但是当我输入正确/错误的东西时,按下提交按钮,文本不会保存在输入字段中,这使我再次输入所有内容。 我是php&的一个菜鸟这里有一些很棒的人帮助我在PHP中验证表单。 另外,我想知道,是否有可能同时使用JS& php一起验证表单? 我在客户端使用验证插件&如果用户已禁用js,则在服务器上运行php。 如果你不介意plz检查表格,如果它确定.. btw plz禁用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>
答案 0 :(得分:0)
当网站重新加载时,表单会被清除。
你必须自己填写。
如果您有这样的输入字段:
<input type="text" name="test" id="test" />
你可以这样做:
<input type="text" name="test" id="test" value="<?= isset($_POST['test']) ? $_POST['test'] : '' ?>" />
已设定值的另一个示例:
<input type="text" name="test2" id="test2" value="Hello" />
变为:
<input type="text" name="test2" id="test2" value="<?= isset($_POST['test2']) ? $_POST['test2'] : 'Hello' ?>" />
检查是否设置了POST字段(在提交表单时发生),然后设置表单字段,否则为空或默认值。
您还可以通过另外检查表单是否无效来进行epand,然后填写字段,否则不填写。
文本区域示例:
<textarea name="testtxt" id="testtxt"></textarea>
变为:
<textarea name="testtxt" id="testtxt"><?= isset($_POST['testtxt']) ? $_POST['testtxt'] : '' ?></textarea>
答案 1 :(得分:0)
名称:输入类型=“文本”名称=“名称”值= php echo $ name;
保持价值: -
value =?php echo $ name;?