Note that there are some explanatory texts on larger screens.

plurals
  1. POCaptcha validation with ajax
    primarykey
    data
    text
    <p>I am working with this tutorial, <a href="http://www.crackajax.net/captchaform.php" rel="nofollow">http://www.crackajax.net/captchaform.php</a>, and having trouble. Neither the form or the captcha will validate. Here is my code for the form:</p> <pre><code>&lt;form action="https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8" method="POST" name="contact" id="contact" onsubmit="return checkform(this);"&gt; ***form elements removed*** &lt;!-- Start Captcha --&gt; &lt;img src="captcha.php" border="0"&gt; &lt;p&gt;Enter Letters:&lt;input type="text" name="code" value=""&gt;&lt;p&gt; &lt;input type="submit" value="Submit Form" onclick="return checkform()"&gt; </code></pre> <p>Here is my script:</p> <pre><code>&lt;script language="JavaScript"&gt; 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; } &lt;/script&gt; </code></pre> <p>And last but not least, here is the captcha.php file:</p> <pre><code>&lt;?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 &lt; $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); ?&gt; </code></pre> <p>The captcha image box will show up and the letters/numbers will also show up but, as I mentioned earlier, the form/captacha will not validate. Once the form is submitted, the user is taken to the thank you page. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload