Note that there are some explanatory texts on larger screens.

plurals
  1. POreturn true for same page validation
    primarykey
    data
    text
    <p>I am having trouble with a same page AJAX/JavaScript/PHP Captcha validation code. The original code is from <a href="http://www.phpcaptcha.org" rel="nofollow">http://www.phpcaptcha.org</a>. We are using a third party site to store all of our form data into a database that is edited by multiple people. Lately we've been receiving a ton of spam so we're trying to implement this Captcha.</p> <p>I'll cut to the chase here. The code is set to 'return false' every time. I need it to 'return true' if certain conditions are met. The code is as follows:</p> <pre><code>&lt;?php session_start(); // this MUST be called prior to any output including whitespaces and line breaks! $GLOBALS['DEBUG_MODE'] = 1; // CHANGE TO 0 TO TURN OFF DEBUG MODE // IN DEBUG MODE, ONLY THE CAPTCHA CODE IS VALIDATED, AND NO EMAIL IS SENT // EMAIL is edited out for school use if( isset($_POST['captcha_code'])) { $a = array("error"=&gt;0); print json_encode($a); } // Process the form, if it was submitted (Original Code called process_si_contact_form()) process_si_zoho1(); ?&gt; &lt;html&gt; &lt;head&gt; &lt;script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/prototype/1.7.0.0/prototype.js"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; //variables not part of original code function reloadCaptcha() { //original code ElementId labled 'captcha_code' document.getElementById('captcha').src ='securimage_show.php?sid=' + Math.random(); } var r, Submit; function processForm() { new Ajax.Request('&lt;?php echo $_SERVER['PHP_SELF'] ?&gt;', { method: 'post', //Original code did not state 'zoho1' parameters: $('zoho1').serialize(), onSuccess: function(transport) { //Re-edited for school use. Not original code try { r = transport.responseText.evalJSON(); Submit = r.submit if (r.error == 0) { alert('Congrats!'); reloadCaptcha(); } else { alert("There was an error with your submission.\n\n" + r.message); } } catch(ex) { alert("There was an error parsing the json"); } }, onFailure: function(err) { alert("Ajax request failed"); } }); return Submit; } } &lt;/script&gt; </code></pre> <p>The process_si-zoho1() is as follows:</p> <pre><code>&lt;?php //Original code process called 'process_si_contact_form()) function process_si_zoho1() { if ($_SERVER['REQUEST_METHOD'] == 'POST' &amp;&amp; @$_POST['do'] == 'contact') { // if the form has been submitted foreach($_POST as $key =&gt; $value) { if (!is_array($key)) { // sanitize the input data if ($key != '-------') $value = strip_tags($value); $_POST[$key] = htmlspecialchars(stripslashes(trim($value))); } } $captcha = $_POST['captcha_code']; // the user's entry for the captcha code $errors = array(); // initialize empty error array if (sizeof($errors) == 0) { require_once dirname(__FILE__) . '/securimage.php'; $securimage = new Securimage(); if ($securimage-&gt;check($captcha) == false) { $errors['captcha_error'] = 'Incorrect security code entered'; } } if (sizeof($errors) == 0) { // no errors, send the form //Edited out mail function from original code //Changed JSON return array on successful validation to send new variable '$Submit' via serialized $entry $Submit = true; $entry = array('error' =&gt; 0, 'submit' =&gt; $Submit); die(json_encode($entry)); } else { $errmsg = $captcha_error; foreach($errors as $key =&gt; $error) { // set up error messages to display with each field $errmsg .= " - {$error}\n"; $Submit = false; } //Added $Submit to the return array $return = array('error' =&gt; 1, 'message' =&gt; $errmsg, 'submit' =&gt; $Submit); die(json_encode($return)); } } // POST } // function process_si_zoho1() ?&gt; </code></pre> <p>The 'processForm()' runs when the submit button is clicked. I'm sure i'm missing something really simple here, I'm just too involved in it. I really appreciate your help</p> <p>I know that the value of 'Submit' is not defined until the PHP in the AJAX.Request() runs but I can't figure out how to define the variable from the start. FYI, the variables 'r' and 'Submit' are all declared outside the function itself so are global variables. If I try to insert a return into the try/catch it will always give me the error in the catch "There was an error parsing the json." Also, with the code as it is now, it will always give me the same error and submit the form anyways, as the value of Submit is blank. Even if I define the Global variable "Submit" as "false" it still returns as though it is blank.</p> <p>If anything other than 'return false' is declared at the bottom of the function, it will submit the form without validating the Captcha. I'm very new to all this, but I've been researching for almost 2 weeks now for 4-8 hours a day and have yet to find a working code. Is it even possible? I mean, other websites use same page validation and submit to third party databases right?</p> <p>I can provide more code if needed, but the problem seems to be here. If I don't try to change the return, the Captcha validates fine and the 'if (r.error == 0)' code executes fine. I have even added an alert to show the value of 'Submit' just to verify the data is transferring between the functions.</p> <p>I'm at my wit's end here. I would appreciate any help.</p> <p>Thanks,</p> <p>Matt</p> <p>The complete code (minus details) is as follows:</p> <pre><code>&lt;?php session_start(); // this MUST be called prior to any output including whitespaces and line breaks! $GLOBALS['DEBUG_MODE'] = 1; // CHANGE TO 0 TO TURN OFF DEBUG MODE // IN DEBUG MODE, ONLY THE CAPTCHA CODE IS VALIDATED, AND NO EMAIL IS SENT // EMAIL is edited out for school use // Process the form, if it was submitted (Original Code called process_si_contact_form()) process_si_zoho1(); ?&gt; &lt;html&gt; &lt;head&gt; &lt;script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/prototype/1.7.0.0/prototype.js"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; //variables not part of original code function reloadCaptcha() { //original code ElementId labled 'captcha_code' document.getElementById('captcha').src = '/securimage_show.php?sid=' + Math.random(); } var r, Submit; function processForm() { new Ajax.Request('&lt;?php echo $_SERVER['PHP_SELF'] ?&gt;', { method: 'post', //Original code did not state 'zoho1' parameters: $('zoho1').serialize(), onSuccess: function(transport) { //Re-edited for school use. Not original code try { r = transport.responseText.evalJSON(); Submit = r.submit; if (r.error == 0) { alert('Congrats!'); reloadCaptcha(); } else { alert("There was an error with your submission.\n\n" + r.message); } } catch(ex) { alert("There was an error parsing the json"); } }, onFailure: function(err) { alert("Ajax request failed"); } }); return false; } } &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;form action="----------" id="zoho1" method="POST" name="leadForm" onsubmit="return processForm()"&gt; &lt;input name="----------" type="hidden" value="----------" /&gt; &lt;input name="----------" type="hidden" value="----------" /&gt; &lt;input name="----------" type="hidden" value="----------" /&gt; &lt;input name="----------" type="hidden" value="----------" /&gt; &lt;input name="----------" type="hidden" value="----------" /&gt; &lt;input name="----------" type="hidden" value="----------" /&gt; &lt;input type="hidden" name="do" value="contact" /&gt;&lt;br /&gt; &lt;p&gt; &lt;label for="First Name"&gt;First Name&lt;/label&gt;&lt;br /&gt; &lt;input class="required" maxlength="40" name="First Name" type="text" /&gt;&lt;/p&gt; &lt;p&gt; &lt;label for="Last Name"&gt;Last Name&lt;/label&gt;&lt;br /&gt; &lt;input class="required" maxlength="80" name="Last Name" type="text" /&gt;&lt;/p&gt; &lt;p&gt; &lt;label email="" for=""&gt;Email&lt;/label&gt;&lt;br /&gt; &lt;input class="required validate-email" maxlength="100" name="Email" type="text" /&gt;&lt;/p&gt; &lt;p&gt; &lt;label for="Phone"&gt;Main Phone&lt;/label&gt;&lt;br /&gt; &lt;input class="required" maxlength="30" name="Phone" type="text" /&gt;&lt;/p&gt; &lt;p&gt; &lt;label for="Mobile"&gt;Mobile Phone&lt;/label&gt;&lt;br /&gt; &lt;input maxlength="30" name="Mobile" type="text" /&gt;&lt;/p&gt; &lt;p&gt; &lt;label for="State"&gt;State&lt;/label&gt;&lt;br /&gt; &lt;select class="required validate-selection" name="State"&gt;&lt;option selected="selected" value="-None-"&gt;-None-&lt;/option&gt;&lt;option value="AL"&gt;AL&lt;/option&gt;&lt;option value="AK"&gt;AK&lt;/option&gt;&lt;option value="AZ"&gt;AZ&lt;/option&gt;&lt;option value="AR"&gt;AR&lt;/option&gt;&lt;option value="CA"&gt;CA&lt;/option&gt;&lt;option value="CO"&gt;CO&lt;/option&gt;&lt;option value="CT"&gt;CT&lt;/option&gt;&lt;option value="DE"&gt;DE&lt;/option&gt;&lt;option value="DC"&gt;DC&lt;/option&gt;&lt;option value="FL"&gt;FL&lt;/option&gt;&lt;option value="HI"&gt;HI&lt;/option&gt;&lt;option value="ID"&gt;ID&lt;/option&gt;&lt;option value="IL"&gt;IL&lt;/option&gt;&lt;option value="IN"&gt;IN&lt;/option&gt;&lt;option value="IA"&gt;IA&lt;/option&gt;&lt;option value="KS"&gt;KS&lt;/option&gt;&lt;option value="KY"&gt;KY&lt;/option&gt;&lt;option value="LA"&gt;LA&lt;/option&gt;&lt;option value="ME"&gt;ME&lt;/option&gt;&lt;option value="MD"&gt;MD&lt;/option&gt;&lt;option value="MA"&gt;MA&lt;/option&gt;&lt;option value="MI"&gt;MI&lt;/option&gt;&lt;option value="MN"&gt;MN&lt;/option&gt;&lt;option value="MS"&gt;MS&lt;/option&gt;&lt;option value="MO"&gt;MO&lt;/option&gt;&lt;option value="MT"&gt;MT&lt;/option&gt;&lt;option value="NE"&gt;NE&lt;/option&gt;&lt;option value="NV"&gt;NV&lt;/option&gt;&lt;option value="NH"&gt;NH&lt;/option&gt;&lt;option value="NJ"&gt;NJ&lt;/option&gt;&lt;option value="NM"&gt;NM&lt;/option&gt;&lt;option value="NY"&gt;NY&lt;/option&gt;&lt;option value="NC"&gt;NC&lt;/option&gt;&lt;option value="ND"&gt;ND&lt;/option&gt;&lt;option value="OH"&gt;OH&lt;/option&gt;&lt;option value="OK"&gt;OK&lt;/option&gt;&lt;option value="OR"&gt;OR&lt;/option&gt;&lt;option value="PA"&gt;PA&lt;/option&gt;&lt;option value="RI"&gt;RI&lt;/option&gt;&lt;option value="SC"&gt;SC&lt;/option&gt;&lt;option value="SD"&gt;SD&lt;/option&gt;&lt;option value="TN"&gt;TN&lt;/option&gt;&lt;option value="TX"&gt;TX&lt;/option&gt;&lt;option value="UT"&gt;UT&lt;/option&gt;&lt;option value="VT"&gt;VT&lt;/option&gt;&lt;option value="VA"&gt;VA&lt;/option&gt;&lt;option value="WA"&gt;WA&lt;/option&gt;&lt;option value="WV"&gt;WV&lt;/option&gt;&lt;option value="WI"&gt;WI&lt;/option&gt;&lt;option value="WY"&gt;WY&lt;/option&gt;&lt;option value="GA"&gt;GA&lt;/option&gt;&lt;/select&gt;&lt;/p&gt; &lt;!--&lt;div&gt;&lt;label for="Mailing Zip"&gt;Mailing Zip&lt;/label&gt;&lt;br /&gt;&lt;input type="text" maxlength="30" name="Mailing Zip" /&gt;&lt;/div&gt;--&gt;&lt;!--&lt;div&gt;&lt;label for="Mailing Country"&gt;Mailing Country&lt;/label&gt;&lt;br /&gt;&lt;input type="text" maxlength="30" name="Mailing Country" /&gt;&lt;/div&gt;--&gt; &lt;p&gt; &lt;label for="----------"&gt;----------&lt;/label&gt;&lt;br /&gt; &lt;select class="required validate-selection" name="----------"&gt;&lt;option selected="selected" value="-None-"&gt;-None-&lt;/option&gt;&lt;option value="----------"&gt;----------&lt;/option&gt;&lt;option value="----------"&gt;----------&lt;/option&gt;&lt;option value="----------"&gt;----------&lt;/option&gt;&lt;option value="----------"&gt;----------&lt;/option&gt;&lt;option value="----------"&gt;----------&lt;/option&gt;&lt;option value="----------"&gt;----------&lt;/option&gt;&lt;option value="----------"&gt;----------&lt;/option&gt;&lt;option value="----------"&gt;----------&lt;/option&gt;&lt;/select&gt;&lt;/p&gt; &lt;p&gt; &lt;label for="-------"&gt;----------&lt;/label&gt;&lt;br /&gt; &lt;select class="required validate-selection" name="-------"&gt;&lt;option selected="selected" value="-None-"&gt;-None-&lt;/option&gt;&lt;option value="----------"&gt;----------&lt;/option&gt;&lt;option value="----------"&gt;----------&lt;/option&gt;&lt;option value="----------"&gt;----------&lt;/option&gt;&lt;option value="----------"&gt;----------&lt;/option&gt;&lt;option value="----------"&gt;----------&lt;/option&gt;&lt;option value="----------"&gt;----------&lt;/option&gt;&lt;/select&gt;&lt;/p&gt; &lt;p&gt; &lt;label for="-------"&gt;------------&lt;/label&gt;&lt;br /&gt; &lt;select class="required validate-selection" name="-------"&gt;&lt;option selected="selected" value="-None-"&gt;-None-&lt;/option&gt;&lt;option value="----------"&gt;----------&lt;/option&gt;&lt;option value="----------"&gt;----------&lt;/option&gt;&lt;option value="----------"&gt;----------&lt;/option&gt;&lt;option value="---------"&gt;-----------&lt;/option&gt;&lt;/select&gt;&lt;/p&gt; &lt;p&gt; &lt;label for="-------"&gt;Intended Degree&lt;/label&gt;&lt;br /&gt; &lt;select class="required validate-selection" name="-------"&gt;&lt;option selected="selected" value="-None-"&gt;-None-&lt;/option&gt;&lt;option value="--------------"&gt;-------------&lt;/option&gt;&lt;option value="-------------"&gt;-------------&lt;/option&gt;&lt;option value="-------------"&gt;--------------&lt;/option&gt;&lt;/select&gt;&lt;/p&gt; &lt;p&gt; &lt;label for="-------"&gt;How did you hear about TTU?&lt;/label&gt;&lt;br /&gt; &lt;textarea class="required" height="250" maxlength="1000" name="-------" width="250"&gt;&lt;/textarea&gt;&lt;/p&gt; &lt;p&gt; &lt;label for="Description"&gt;Comments&lt;/label&gt;&lt;br /&gt; &lt;textarea height="250" maxlength="1000" name="Description" width="250"&gt;&lt;/textarea&gt;&lt;/p&gt; &lt;img id="captcha" src="/securimage_show.php" alt="CAPTCHA IMAGE" /&gt; &lt;input type="text" id="enterVerify" name="captcha_code" size="10" maxlength="6" /&gt; &lt;input type="button" id="reload" name="Reload" value="Reload" onClick="reloadCaptcha()"&gt; &lt;input class="form-button" name="save" type="submit" value="Submit" /&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; &lt;?php //Original code process called 'process_si_contact_form()) function process_si_zoho1() { if ($_SERVER['REQUEST_METHOD'] == 'POST' &amp;&amp; @$_POST['do'] == 'contact') { // if the form has been submitted foreach($_POST as $key =&gt; $value) { if (!is_array($key)) { // sanitize the input data if ($key != '-------') $value = strip_tags($value); $_POST[$key] = htmlspecialchars(stripslashes(trim($value))); } } $captcha = $_POST['captcha_code']; // the user's entry for the captcha code $errors = array(); // initialize empty error array if (sizeof($errors) == 0) { require_once dirname(__FILE__) . '/securimage.php'; $securimage = new Securimage(); if ($securimage-&gt;check($captcha) == false) { $errors['captcha_error'] = 'Incorrect security code entered'; } } if (sizeof($errors) == 0) { // no errors, send the form //Edited out mail function from original code //Changed JSON return array on successful validation to send new variable '$Submit' via serialized $entry $Submit = true; $entry = array('error' =&gt; 0, 'submit' =&gt; $Submit); die(json_encode($entry)); } else { $errmsg = $captcha_error; foreach($errors as $key =&gt; $error) { // set up error messages to display with each field $errmsg .= " - {$error}\n"; $Submit = false; } //Added $Submit to the return array $return = array('error' =&gt; 1, 'message' =&gt; $errmsg, 'submit' =&gt; $Submit); die(json_encode($return)); } } // POST } // function process_si_zoho1() ?&gt; </code></pre>
    singulars
    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