如何使用ajax更改capcha脚本?

时间:2015-12-30 01:50:26

标签: jquery ajax

抱歉,如果这太容易掌握了。我有这个代码 index.html

<link rel="stylesheet" href="jquery.realperson.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="jquery.plugin.min.js"></script>
<script src="jquery.realperson.min.js"></script>

<script>
$(function() {
    $('#defaultReal').realperson();
});
</script>

<div id="lalala">
</div>
    <form action="jquery.realperson.php" method="post">
        <input type="text" id="defaultReal" name="defaultReal">
        <p style="clear: both;">
            <label>&nbsp;</label>
            <input type="submit" class="Submit" value="Submit">
        </p>
    </form>

jquery.realperson.php

<?php
function rpHash($value) {
    $hash = 5381;
    $value = strtoupper($value);
    for($i = 0; $i < strlen($value); $i++) {
        $hash = (($hash << 5) + $hash) + ord(substr($value, $i));
    }
    return $hash;
}
if (rpHash($_POST['defaultReal']) == $_POST['defaultRealHash']) {
echo 'OK';
} else {
echo 'NO';
}
?>

所以我想这样做。打电话给php

$.ajax({
    type: "POST",
    url: "jquery.realperson.php",
    data: dataString,
    cache: false,
    success: function(html) {
    if (html == 'OK') {
        $("#lalala").html("ok");
    } else {
        $("#lalala").html("no");
    }
});

如何使用ajax?因为如果我使用上面的内容,我将页面移动到 jquery.realperson.php 。所以我想继续 index.html ...我得到这个代码形式http://keith-wood.name/realPerson.html

1 个答案:

答案 0 :(得分:1)

提交表单时调用ajax并取消提交的默认行为。

$('form').on('submit', function(e) {
  e.preventDefault();

  $.ajax({
    type: this.method, // use the method specified in the form
    url: this.action, // use the url (action) specified in the form
    data: dataString, // dataString is currently undefined
    cache: false,
    success: function(html) {
      if (html == 'OK') {
        $("#lalala").html("ok");
      } else {
        $("#lalala").html("no");
      }
    }
  });

});

您的代码目前使用未定义的dataString变量,您需要修改该变量才能使代码正常工作。

相关问题