我正在尝试创建一个使用验证码的ajax联系人webform。没有验证码的表单工作正常,我也可以让验证码自行工作(不通过ajax验证)。
这是我的forms.js这部分专门验证表单,然后将信息发布到php文件
submitFu:function(){
_.validateFu()
if(!_.form.has('.'+_.invalidCl).length)
$.ajax({
type: "POST",
url:_.mailHandlerURL,
data:{
name:$('.name input',_.form).val()||'nope',
email:$('.email input',_.form).val()||'nope',
phone:$('.phone input',_.form).val()||'nope',
message:$('.message textarea',_.form).val()||'nope',
owner_email:_.ownerEmail,
stripHTML:_.stripHTML
},
success: function(){
_.showFu()
}
})
},
showFu:function(){
_.form.trigger('reset')
_.success.slideDown(function(){
setTimeout(function(){
_.success.slideUp()
},_.successShow)
})
},
表示captcha.php
session_start();
//Check if the securidy code and the session value are not blank
//and if the input text matches the stored text
if ( ($_REQUEST["txtCaptcha"] == $_SESSION["security_code"]) &&
(!empty($_REQUEST["txtCaptcha"]) && !empty($_SESSION["security_code"])) ) {
echo "<h1>True</h1>";
}
else {
echo "<h1>Incorrect Security Code</h1>";
}
这里是表单的html(结果div意味着显示不正确的验证码)
<h4>Contact Form</h4>
<form id="contact-form">
<div class="success">Contact form submitted! We will be in touch soon </div>
<fieldset>
<label class="name">
<input type="text" value="Name:">
<span class="error">*This is not a valid name.</span>
<span class="empty">*This field is required.</span>
</label>
<label class="email">
<input type="text" value="E-mail:">
<span class="error">*This is not a valid email address.</span>
<span class="empty">*This field is required.</span>
</label>
<label class="phone">
<input type="text" value="Phone:">
<span class="error">*This is not a valid phone number.</span>
<span class="empty">*This field is required.</span>
</label>
<label class="message">
<textarea>Message:</textarea>
<span class="error">*The message is too short.</span>
<span class="empty">*This field is required.</span>
</label>
<label for="captcha">Captcha</label>
<input id="txtCaptcha" type="text" name="txtCaptcha" value="" maxlength="10" size="32" />
<img id="imgCaptcha" src="create_image.php" />
<div id="result"> </div>
<div class="btns">
<a class="button" data-type="reset">clear</a><a class="button" data-type="submit">send</a></div>
</fieldset>
所以我缺少的是验证码部分的脚本。我已经尝试了以下脚本,它可以自行工作(不是表单的一部分,并有自己的提交按钮)但我似乎无法使用上面发布的forms.js
function getXmlHttpRequestObject() {
if (window.XMLHttpRequest) {
return new XMLHttpRequest(); //Mozilla, Safari ...
} else if (window.ActiveXObject) {
return new ActiveXObject("Microsoft.XMLHTTP"); //IE
} else {
//Display our error message
alert("Your browser doesn't support the XmlHttpRequest object.");
}
}
//Our XmlHttpRequest object
var receiveReq = getXmlHttpRequestObject();
//Initiate the AJAX request
function makeRequest(url, param) {
//If our readystate is either not started or finished, initiate a new request
if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
//Set up the connection to captcha_test.html. True sets the request to asyncronous(default)
receiveReq.open("POST", url, true);
//Set the function that will be called when the XmlHttpRequest objects state changes
receiveReq.onreadystatechange = updatePage;
receiveReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
receiveReq.setRequestHeader("Content-length", param.length);
receiveReq.setRequestHeader("Connection", "close");
//Make the request
receiveReq.send(param);
}
}
//Called every time our XmlHttpRequest objects state changes
function updatePage() {
//Check if our response is ready
if (receiveReq.readyState == 4) {
//Set the content of the DIV element with the response text
document.getElementById('result').innerHTML = receiveReq.responseText;
//Get a reference to CAPTCHA image
img = document.getElementById('imgCaptcha');
//Change the image
img.src = 'create_image.php?' + Math.random();
}
}
//Called every time when form is performed
function getParam(theForm) {
//Set the URL
var url = 'captcha.php';
//Set up the parameters of our AJAX call
var postStr = theForm.txtCaptcha.name + "=" + encodeURIComponent( theForm.txtCaptcha.value );
//Call the function that initiate the AJAX request
makeRequest(url, postStr);
我知道这很长并且有很多信息。非常感谢您的时间和帮助 }
答案 0 :(得分:0)
为什么不使用reCaptcha作为验证码?它们具有出色的API,并且已被证明是最安全的验证码。