我有點卡住如何正確驗證這種形式。實際上,它的工作類型,但是當我輸入正確/錯誤的東西時,然後按提交按鈕,文本不會保存在輸入字段中,這使我再次輸入所有內容。 我是一個在PHP &的菜鳥,這裏的一些偉人幫助我在PHP中驗證表單。 另外,我想知道,是否可以同時使用JS & php來驗證表單? 我在客戶端使用驗證插件& php在服務器上,如果用戶已禁用js。 如果你不介意plz檢查表格,如果它確定.. btw plz禁用js在php檢查表單時看到問題。 https://www.dropbox.com/s/vka7cy70xv4zryf/formtest.zip 謝謝你的時間!按下提交按鈕時,HTML輸入字段不保存文本?
<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('[email protected]',$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>
請不要在此處添加潛在的惡意壓縮附件。在問題中發佈代碼。 – Zlatko
嗨, 沒有它的不惡意,不包含任何病毒或類似的東西,我只是上傳,只爲視覺效果。 我現在會發布代碼 – user2120702
你可以這樣寫:'」/>'這樣你就可以在提交後保留表單中的數據。 – ashley