我是 jQuery 的新手。
我有一个带有验证码的 html 联系表单。
该表单使用 jQuery 来验证姓名、电子邮件地址和消息。
但是我在集成验证码验证时遇到了麻烦。
verifyimage.php
使用以下代码(通过)生成验证码:
<?php
session_start();
error_reporting( 0 );
if( !function_exists( "imagecreatefrompng" ) )
{
exit( "You need to recompile with the GD library included in PHP for this feature to be able to function" );
}
$alphanum = "ABCDEFGHIJKLMNPQRSTUVWXYZ123456789";
$rand = substr( str_shuffle( $alphanum ), 0, 6 );
$image = imagecreatefrompng( "../images/verify.png" );
$textColor = imagecolorallocate( $image, 150, 150, 150 );
imagestring( $image, 5, 14, 22, $rand, $textColor );
$_SESSION['image_random_value'] = md5( $rand );
header( "Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
header( "Last-Modified: ".gmdate( "D, d M Y H:i:s" )." GMT" );
header( "Cache-Control: no-store, no-cache, must-revalidate" );
header( "Cache-Control: post-check=0, pre-check=0", false );
header( "Pragma: no-cache" );
header( "Content-type: image/png" );
imagepng( $image );
imagedestroy( $image );
?>
表单的 HTML 如下:
<!-- Contact Form -->
<div id="contactForm">
<form action="/contact.php" method="post">
<div class="oneThird">
<div class="row">
<label for="contactName">Full Name</label>
<input id="contactName" name="contactName" type="text" />
</div>
<div class="row topMargin">
<label for="contactEmail">Email Address</label>
<input id="contactEmail" name="contactEmail" type="text" />
</div>
<div class="row topMargin">
<label for="contactVerify">Captcha</label>
<input style="background-image: url(/php/verifyimage.php);" id="contactVerify" name="contactVerify" type="text" />
</div>
</div>
<div class="twoThird lastColumn">
<div class="row">
<label for="contactMessage">Message</label>
<textarea id="contactMessage" name="contactMessage" cols="10" rows="9"></textarea>
</div>
</div>
<div class="clearfix">
</div>
<button type="submit" class="colorButton">Send Message</button>
</form>
</div>
jQuery 包含以下与验证表单名称、电子邮件和消息部分相关的代码:
// Contact
if ($('#contactForm').length != 0)
{
var labelName = $('#contactForm label[for="contactName"]').text();
var labelEmail = $('#contactForm label[for="contactEmail"]').text();
var labelMessage = $('#contactForm label[for="contactMessage"]').text();
var emailPattern = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
if ($('#contactVerify').length != 0)
{
var labelVerify = $('#contactForm label[for="contactVerify"]').text();
}
$('#contactForm button').click(function()
{
if ($('#contactName').val() == '')
{
$('#contactForm label[for="contactName"]').addClass('error').text(labelName + ' is required');
}
else
{
$('#contactForm label[for="contactName"]').removeClass('error').text(labelName);
}
if ($('#contactEmail').val() == '')
{
$('#contactForm label[for="contactEmail"]').addClass('error').text(labelEmail + ' is required');
}
else if (!emailPattern.test($('#contactEmail').val()))
{
$('#contactForm label[for="contactEmail"]').addClass('error').text(labelEmail + ' is invalid');
}
else
{
$('#contactForm label[for="contactEmail"]').removeClass('error').text(labelEmail);
}
if ($('#contactMessage').val() == '')
{
$('#contactForm label[for="contactMessage"]').addClass('error').text(labelMessage + ' is required');
}
else
{
$('#contactForm label[for="contactMessage"]').removeClass('error').text(labelMessage);
}
if (typeof labelVerify != 'undefined')
{
if ($('#contactVerify').val() == '')
{
$('#contactForm label[for="contactVerify"]').addClass('error').text(labelVerify + ' is required');
}
else
{
$('#contactForm label[for="contactVerify"]').removeClass('error').text(labelVerify);
}
}
if ($('#contactForm label.error').length == 0)
{
$('#contactForm form').append('<input type="hidden" value="1" name="contactValid" />');
}
else
{
return false;
}
});
}
我对如何实现验证码验证感到困惑。我是否将其包含在contact.php
发布到的表单中?
或者我可以在表格发布之前验证验证码吗?就像电子邮件地址一样?
我试过添加:
<?php
session_start();
$cap = 'notEq';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (md5($_POST['contactVerify']) == $_SESSION['image_random_value']) {
// contactVerify verification is Correct. Do something here!
$cap = 'Eq';
} else {
// contactVerify verification is wrong. Take other action
$cap = '';
}
}
?>
到 HTML 表单的顶部。然后还添加:
var capch = '<?php echo $cap; ?>';
if(capch != 'notEq'){
if(capch == 'Eq'){
// My PHPMailer script //
}
else{
$('#contactForm label[for="contactVerify"]').addClass('error').text(labelVerify + ' is invalid');
}
}
});
作为脚本。但没有任何喜悦。无论验证码是否无效,都会发送表单。
任何帮助表示赞赏。