Note that there are some explanatory texts on larger screens.

plurals
  1. POSame page Captcha validation using AJAX
    primarykey
    data
    text
    <p>Ok, So i've been working on this code for a while now. I started with a preset code from SecurImages and have been tweaking it to try to validate the captcha AND submit the form. The catch is, our form is submitted to a third party site who created it and submits the data into their own database and I don't have access to their code. They use several hidden fields to pull information, for example, they link to their general website and then check that the page was submitted through a form then check the submission details to determine which form and where to submit the info. My problem is, I can either get the captcha to validate and not submit the form, or I can submit the form and not validate the captcha. I can't seem to do both. At current, I can validate the captcha properly, but the form doesn't submit correctly.</p> <pre><code> function processForm() { new Ajax.Request('&lt;?php echo $_SERVER['PHP_SELF'] ?&gt;', { method: 'post', parameters: $('zoho1').serialize(), onSuccess: function(transport) { try { var r = transport.responseText.evalJSON(); if (r.error == 0) { alert ("You are the man!"); } 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; } </code></pre> <p>When the user clicks the submit button it runs this AJAX query.</p> <pre><code>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 != 'LEADCF3') $value = strip_tags($value); $_POST[$key] = htmlspecialchars(stripslashes(trim($value))); } } /* $name = @$_POST['First Name'] . @$_POST['Last Name']; // name from the form $email = @$_POST['Email']; // email from the form $mainphone = @$_POST['Phone']; // url from the form $mobile = @$_POST['Mobile']; // the message from the form $state = @$_POST['State']; // the state from the form $type = @$_POST['LEADCF4']; // Type of student from form $enrollment = @$_POST['LEADCF2']; //Expected enrollment $time = @$_POST['LEADCF5']; //Intended enrollment status $degree = @$_POST['LEADCF6']; //Intended degree $message = @$_POST['LEADCF3']; //How did you hear about TTU? $comments = @$_POST['Description']; //Comments*/ $captcha = $_POST['captcha_code']; // the user's entry for the captcha code //$name = substr($name, 0, 64); // limit name to 64 characters $errors = array(); // initialize empty error array if (isset($GLOBALS['DEBUG_MODE']) &amp;&amp; $GLOBALS['DEBUG_MODE'] == false) { // only check for errors if the form is not in debug mode if (strlen($email) == 0) { // no email address given $errors['email_error'] = 'Email address is required'; } else if ( !preg_match('/^(?:[\w\d]+\.?)+@(?:(?:[\w\d]\-?)+\.)+\w{2,4}$/i', $email)) { // invalid email format $errors['email_error'] = 'Email address entered is invalid'; } if (strlen($message) &lt; 10) { // message length too short $errors['message_error'] = 'Please enter a message'; } } // Only try to validate the captcha if the form has no errors // This is especially important for ajax calls 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 // header('http://crm.zoho.com/crm/WebToLeadForm'); /*if (isset($GLOBALS['DEBUG_MODE']) &amp;&amp; $GLOBALS['DEBUG_MODE'] == false) { //send the message with mail() header('location:http://www.tntemple.edu/request-information-online-learning-thank-you'); }*/ $return = array('error' =&gt; 0, 'message' =&gt; 'OK'); die(json_encode($return)); } else { $errmsg = $captcha_error; foreach($errors as $key =&gt; $error) { // set up error messages to display with each field $errmsg .= " - {$error}\n"; } $return = array('error' =&gt; 1, 'message' =&gt; $errmsg); die(json_encode($return)); } } // POST } // function process_si_zoho1() </code></pre> <p>This function runs immediately when the page is loaded and checks if the page was submitted. The commented out variables were originally part of the code, but I decided we didn't need them so I commented them out. The original code was intended to mail the results of the form to a recipient but we need it to "return true" to submit the form.</p> <pre><code>&lt;form action="https://crm.zoho.com/crm/WebToLeadForm" id="zoho1" method="POST" name="leadForm" onsubmit="return processForm()"&gt; </code></pre> <p>This is the form with the action and onSubmit. I hope i've been clear enough. Thanks for the help. I'm fairly new to programming outside of HTML but I pick up really quickly most of the time.</p> <p>also, ideally we will be redirecting to another URL upon successful submission of the form.</p>
    singulars
    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