使用AJAX进行相同的Captcha验证

时间:2013-05-01 15:11:39

标签: php javascript ajax captcha

好的,我现在已经在这段代码上工作了一段时间。我从SecurImages的预设代码开始,并一直在调整它以尝试验证验证码并提交表单。问题是,我们的表单被提交给创建它的第三方站点,并将数据提交到他们自己的数据库中,我无法访问他们的代码。他们使用几个隐藏字段来提取信息,例如,他们链接到他们的一般网站,然后检查页面是否通过表单提交,然后检查提交详细信息以确定提交信息的形式和位置。我的问题是,我可以让验证码验证而不是提交表格,或者我可以提交表格而不验证验证码。我似乎无法做到这两点。目前,我可以正确验证验证码,但表格没有正确提交。

        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;
    }

当用户点击提交按钮时,它会运行此AJAX查询。

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()

此功能在加载页面时立即运行,并检查页面是否已提交。注释掉的变量最初是代码的一部分,但我认为我们并不需要它们所以我对它们进行了评论。原始代码旨在将表单的结果邮寄给收件人,但我们需要它来回复真实&#34;提交表格。

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

这是包含action和onSubmit的表单。我希望我已经足够清楚了。谢谢您的帮助。我在HTML之外的编程方面相当新,但我大部分时间都很快就接受了。

另外,理想情况下,我们将在成功提交表单后重定向到另一个URL。

1 个答案:

答案 0 :(得分:0)

基本上,为了返回表单的动作,我刚刚添加了

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

它返回到操作字段,就好像没有参数的情况下按下提交按钮一样,“return false”