所以我用recaptcha等创建了一个简单的表单,我希望我的验证错误消息显示在所需字段的顶部或旁边。
目前验证发生在服务器端,因此在用户单击提交后,它们将被带到我的.php并显示缺少的需求字段。这是我的contact.php
<?php
if(isset($_POST['email'])) {
$email_to = "myemail";
$email_subject = "Contact";
require_once('recaptchalib.php');
$privatekey = "myprivatekey";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if (!$resp->is_valid) {
// What happens when the CAPTCHA was entered incorrectly
die ("Please return to the form and enter the numbers you see in the image." .
"(reCAPTCHA said: " . $resp->error . ")");
} else {
}
function died($error) {
//error message
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
if(!isset($_POST['name']) ||
!isset($_POST['email']) ||
!isset($_POST['message'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$name = $_POST['name']; // required
$email = $_POST['email']; // required
$message = $_POST['message']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$name)) {
$error_message .= 'You must enter your name.<br />';
}
if (empty($_POST["message"])) {
$error_message .= 'You must have something to ask us.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Name: ".clean_string($name)."\n";
$email_message .= "Email: ".clean_string($email)."\n";
$email_message .= "Message: ".clean_string($message)."\n";
$headers = 'From: '.$email."\r\n".
'Reply-To: '.$email."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
header("Location: http://tahnkyoupage.html");
?>
<?php
}
die();
?>
这是我在contact.html上的html
<div class="form_settings">
<form name="contact" method="post" action="contact.php">
<p><span>Name</span><input class="contact" type="text" name="name" value="" /></p>
<p><span>Email Address</span><input class="contact" type="text" name="email" value="" /></p>
<p><span>Message</span><textarea class="contact" rows="8" cols="50" name="message"></textarea></p>
<script type="text/javascript" src="http://www.google.com/recaptcha/api/challenge?k=6LfJw_cSAAAAANWyfiEoquxwiNNGUbAyMMFZcdk9"></script>
<noscript>
<iframe src="http://www.google.com/recaptcha/api/noscript?k=6LfJw_cSAAAAANWyfiEoquxwiNNGUbAyMMFZcdk9"
height="300" width="500" frameborder="0"></iframe><br>
<textarea name="recaptcha_challenge_field" rows="3" cols="40">
</textarea>
<input type="hidden" name="recaptcha_response_field" value="manual_challenge">
</noscript>
<p style="padding-top: 15px"><span> </span><input class="submit" type="submit" name="contact_submitted" value="Send" /></p>
</div><!--close form_settings-->
我假设我必须将$ error_message变量放在某个地方,但我不确定在哪里......有很多方法可以进行验证,我现在已经迷失了,因为我可以简单地显示哪些字段需要填写无需将用户带到另一个页面 谢谢 ; &GT;