不会在AJAX中返回正确的值

时间:2016-02-23 15:47:46

标签: php ajax

我有这个问题。我无法获得txtHint的正确值。 我有一个名为balance的数据库,其中有balance_idstudent_idtotal_balance

这是代码行:

<script>
function showHint(str) {
    if (str.length == 0) {
        document.getElementById("txtHint").innerHTML = "";
        return;
    } else {
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("txtHint").value = xmlhttp.responseText;
        }
    };
    xmlhttp.open("GET", "getuser.php?q=" + str, true);
    xmlhttp.send();
    }
}
</script>

<input type="text" class="input-block-level" id="student_idx" name="student_idx" onkeyup="showHint(this.value)">
<span><input id="txtHint" name="txtHint" type = "hidden"></span>
<button name="save" class="btn btn-info"><i class="icon-plus-sign icon-large">



if (isset($_POST['save'])) {

            $query = mysql_query("select * from student as st,balance ORDER BY st.student_id DESC") or die(mysql_error());

              $amount = $_POST['amount'];
              $student_idx = $_POST['student_idx'];
              $balance = $_POST['txtHint'];

                  $difference = $balance - $amount;

                  mysql_query("UPDATE balance SET total_balance = '$difference' WHERE student_id = '$student_idx'")or die(mysql_error());

?>

<?php } ?>

这是getuser.php

的代码
<?php include('dbcon.php'); ?>
<?php
$q = intval($_GET['q']);
$query = "SELECT * FROM balance WHERE student_id = '".$q."'";
$result = mysql_query($query);

while($row = mysql_fetch_array($result)){
    echo $row['balance'];
}
?>

问题是,当我的total_balance为150,并且我支付了50的金额时,结果为-50。我不知道这有什么不对。

0 个答案:

没有答案