[英]Problems with PHP form validation
我的表格分布在兩頁中。 使用稱為Slick的JS滑塊打開頁面。 填寫表格並單擊第2頁上的“發送”按鈕后,驗證提示我沒有填寫姓名。 您可以在以下網站上親自查看此內容: Rofordaward 。 只需填寫提名表格並推送即可。 下面的HTML和PHP代碼。
的HTML
SNIPPET OF FORM (from nominate.html): <form action="nominate.php" method="post"> <div class="single-item slider"> <div class="page1"> <label class="row"> <h2 class="headline">Your full name</h2> <input type="text" name="yourname" placeholder="Forename and surname"></input> </label> <label class="row email"> <h2 class="headline">Your email address <p>Don't worry, we won't spam you or share your email address</p></h2> <input type="text" name="email" placeholder="example@rofordaward.co.uk"></input> </label> <label class="row"> <h2 class="headline">Name of company</h2> <input type="text" name="companyname" placeholder="eg Roford"></input> </label> </div> <div class="page2"> <label class="row reason"> <h2 class="headline">Reason for nomination</h2> <textarea id="textarea" rows="6" cols="25" maxlength="1000" name="reason" placeholder="A brief evidence based summary"></textarea> <div id="text-area-wrap"> <div id="textarea_feedback"></div> </div> </label> <div class="row button-wrap"> <div class="column small-12"> <input class="button" type="submit" value="Send it!"> </div> </div> </div> </div> </form>
的PHP
/* Check all form inputs using check_input function */
$yourname = check_input($_POST['yourname'], "Enter your name");
$email = check_input($_POST['email']);
$companyname = check_input($_POST['companyname']);
$reason = check_input($_POST['reason'], "Write your reason");
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
show_error("E-mail address not valid");
}
/*Message for the e-mail */
$message = "New submission
Name: $yourname
E-mail: $email
Company name: $companyname
Reason:
$reason
End of message
";
/* Send the message using mail() function */
mail($myemail, $subject, $message);
/* Redirect visitor to the thank you page */
header('Location: thanks.htm');
exit();
/* Functions used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
?>
<html>
<body>
<b>Please correct the following error:</b><br />
<?php echo $myError; ?>
</body>
</html>
<?php
exit();
}
?>
當我刪除JS滑塊的腳本時,該表單將完全起作用。
在您的PHP代碼中,您正在使用$_POST['email']
,根據您在上一個HTML塊上的代碼,當<input type="text" name="email" />
設置為'email'
時,此代碼'email'
另一方面,在您的第一個HTML代碼塊中:
<label class="row email">
....
<input type="text" name="youremail" ....></input>
</label>
您設置name="youremail"
,這肯定會使您的PHP代碼得出錯誤的結果。 您應該像在PHP代碼中那樣使用$_POST['youremail']
來使其正常工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.