JQuery验证&真实的验证码

时间:2016-01-30 21:57:33

标签: javascript php jquery forms

我正在尝试提交表单&我正在使用jquery realperson作为验证码。问题是realperson字段的哈希没有被提交。我想验证表格,包括验证码,并在同一页面上返回结果,无论是否成功。

这是我的example.html

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>jQuery Real Person</title>
<link rel="stylesheet" href="jquery.realperson.css">
<style>
label { display: inline-block; width: 20%; }
.realperson-challenge { display: inline-block }
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="jquery.plugin.js"></script>
<script src="jquery.realperson.js"></script>
<script src="jquery.validate.min.js"></script>
<script src="form-validate.js"></script>
<script>
$(function() {
$('#defaultReal').realperson();
});
</script>
</head>
<body>
<h1>Example</h1>
<p>&nbsp;</p>
<form action="formProcess.php" method="post" id="myForm" name="myForm">
<p><label>Other fields:</label><input type="text" id="field" name="field"></p>
<p><label>Please enter the letters displayed:</label>
    <input type="text" id="defaultReal" name="defaultReal"></p>
<p style="clear: both;"><label>&nbsp;</label><input type="submit" value="Submit"></p>
<div id="result"></div>
</form>
</body>
</html>

这是我的自定义表单提交:

    $.validator.setDefaults({
    submitHandler: function() {}
});

$(document).ready(function(){
            //form validation rules
            $("#myForm").validate({
                rules: {
                    field: "required"
                    },
                messages: {
                    field: "Please enter something"
                },
                submitHandler: function(form) {
                $("#result").html('');
                postForm();
             }

            });

function postForm() {
    var ajaxRequest;
    $("#result").html('');
    var values = $("#myForm").serialize()
    ajaxRequest= $.ajax({
            url: "formProcess.php",
            type: "post",
            data: values
        });

     ajaxRequest.done(function (response, textStatus, jqXHR){
          // show successfully for submit message
          $("#result").html(response);
     });

     /* On failure of request this function will be called  */
     ajaxRequest.fail(function (){
       // show error
       $("#result").html('There was an error!');
     });

    }


});

我的php processForm:

    <?php
function rpHash($value) {
    $hash = 5381;
    $value = strtoupper($value);
    for($i = 0; $i < strlen($value); $i++) {
        $hash = (leftShift32($hash, 5) + $hash) + ord(substr($value, $i));
    }
    return $hash; }

function leftShift32($number, $steps) {
    $binary = decbin($number);
    $binary = str_pad($binary, 32, "0", STR_PAD_LEFT);
    $binary = $binary.str_repeat("0", $steps);
    $binary = substr($binary, strlen($binary) - 32);
    return ($binary{0} == "0" ? bindec($binary) :
        -(pow(2, 31) - bindec(substr($binary, 1)))); 
}

if (rpHash($_POST['defaultReal']) == $_POST['defaultRealHash']) {

    echo "Security was Correct!";
}
else {
    echo "Security captcha incorrect!";
}

?>

0 个答案:

没有答案