0

我正在使用本教程http://www.crackajax.net/captchaform.php,但遇到了麻烦。表单或验证码都不会验证。这是我的表单代码:

<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);
?>

验证码图像框将显示,字母/数字也将显示,但正如我之前提到的,表单/验证码不会验证。提交表单后,用户将被带到感谢页面。

4

1 回答 1

0

我从教程中复制了代码,它可以工作。你没有说 captcheck.php 文件,它存在吗?使用教程中的 captcheck.php 和我必须删除的代码

if(document.contact.req.value=='') {
    alert('Please complete the "required" field');
    document.contact.req.focus();
    return false;
}

让它工作。

于 2012-06-09T23:58:26.503 回答