Same page Captcha validation using AJAX

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.

        function processForm()
    {
        new Ajax.Request('<?php echo $_SERVER['PHP_SELF'] ?>', {
            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;
    }

When the user clicks the submit button it runs this AJAX query.

function process_si_zoho1()
{
if ($_SERVER['REQUEST_METHOD'] == 'POST' && @$_POST['do'] == 'contact') {
    // if the form has been submitted

    foreach($_POST as $key => $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']) && $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) < 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->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']) && $GLOBALS['DEBUG_MODE'] == false) {
            //send the message with mail()
            header('location:http://www.tntemple.edu/request-information-online-learning-thank-you');
        }*/

        $return = array('error' => 0, 'message' => 'OK');
        die(json_encode($return));
    } else {
        $errmsg = $captcha_error;
        foreach($errors as $key => $error) {
            // set up error messages to display with each field
            $errmsg .= " - {$error}\n";
        }

        $return = array('error' => 1, 'message' => $errmsg);
        die(json_encode($return));
    }


} // POST
} // function process_si_zoho1()

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.

<form action="https://crm.zoho.com/crm/WebToLeadForm" id="zoho1" method="POST" name="leadForm" onsubmit="return processForm()">

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.

also, ideally we will be redirecting to another URL upon successful submission of the form.

Answers


Basically, in order to return the action of the form, I just added

if (r.error == 0) {
reloadCaptcha()
document.forms['formname'].submit()

It returns to the action field, as if the submit button was pressed without the argument, "return false"


Need Your Help

How do I Set a Key and a Value in a JFace ComboViewer?

java swt derby jface

I am trying to set up a combo box that will display a UserName, but when selected will send a an ID instead.

How to configure Jenkins to detect SVN changes and execute a job?

c++ linux svn jenkins cmake

I have configured Jenkins and created a job to checkout, build and run a simple project from SVN. I could configure this job to run periodically, like once every five minutes. But I'd like it to bu...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.