我正在使用本教程,http://www.crackajax.net/captchaform.php,並且遇到問題。表單或驗證碼都不會被驗證。這裏是我的表單代碼:用ajax驗證碼驗證碼
<form action="https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8" method="POST" name="contact" id="contact" onsubmit="return checkform(this);">
***form elements removed***
<!-- Start Captcha -->
<img src="captcha.php" border="0">
<p>Enter Letters:<input type="text" name="code" value=""><p>
<input type="submit" value="Submit Form" onclick="return checkform()">
這裏是我的腳本:
<script language="JavaScript">
var url = 'captcheck.php?code=';
var captchaOK = 2; // 2 - not yet checked, 1 - correct, 0 - failed
function getHTTPObject()
{
try {
req = new XMLHttpRequest();
} catch (err1)
{
try {
req = new ActiveXObject("Msxml12.XMLHTTP");
} catch (err2)
{
try {
req = new ActiveXObject("Microsoft.XMLHTTP");
} catch (err3)
{
req = false;
}
}
}
return req;
}
var http = getHTTPObject(); // We create the HTTP Object
function handleHttpResponse() {
if (http.readyState == 4) {
captchaOK = http.responseText;
if(captchaOK != 1) {
alert('The entered code was not correct. Please try again');
document.contact.code.value='';
document.contact.code.focus();
return false;
}
document.contact.submit();
}
}
function checkcode(thecode) {
http.open("GET", url + escape(thecode), true);
http.onreadystatechange = handleHttpResponse;
http.send(null);
}
function checkform() {
// First the normal form validation
if(document.contact.req.value=='') {
alert('Please complete the "required" field');
document.contact.req.focus();
return false;
}
if(document.contact.code.value=='') {
alert('Please enter the string from the displayed image');
document.contact.code.value='';
document.contact.code.focus();
return false;
}
// Now the Ajax CAPTCHA validation
checkcode(document.contact.code.value);
return false;
}
</script>
最後但並非最不重要的,這裏是captcha.php文件:
<?php
//Start a session so we can store the captcha code as a session variable.
session_start();
// Decide what characters are allowed in our string
// Our captcha will be case-insensitive, and we avoid some
// characters like 'O' and 'l' that could confuse users
$charlist = '23456789ABCDEFGHJKMNPQRSTVWXYZ';
// Trim string to desired number of characters - 5, say
$chars = 5;
$i = 0;
while ($i < $chars)
{
$string .= substr($charlist, mt_rand(0, strlen($charlist)-1), 1);
$i++;
}
// Create a GD image from our background image file
$captcha = imagecreatefrompng('captcha.png');
// Set the colour for our text string
// This is chosen to be hard for machines to read against the background, but
// OK for humans
$col = imagecolorallocate($captcha, 240, 200, 240);
// Write the string on to the image using TTF fonts
imagettftext($captcha, 17, 0, 13, 22, $col, 'Carton-Slab.otf', $string);
// Store the random string in a session variable
$_SESSION['secret_string'] = $string;
// Put out the image to the page
header("Content-type: image/png");
imagepng($captcha);
?>
的應用驗證碼圖像框會顯示出來,字母/數字也會顯示出來,但正如我前面提到的,表單/ captacha不會被驗證。表單提交後,用戶將被帶到感謝頁面。
考慮用jQuery $ .ajax替換那個古老的XMLHttpRequest對象作爲開始;減少這樣的代碼總會讓問題看起來更小。 – 2012-06-09 17:41:49
我對js/ajax沒有太多經驗,所以如果在XMLHttpRequest對象中找不到問題,我認爲我會保持原樣,直到驗證工作。 (還是)感謝你的建議。 – user715564