PHP,JS,AJAX无法向DB发送值

时间:2014-02-19 17:52:27

标签: javascript php jquery ajax database

我正在创建一个可以拖动图像的网站。然后我有一个Javascript代码,用于检测用户按x和y坐标拖动页面的位置。为此,我有这个代码:(house_position.js)

    var offset = 0;

var xPos = 0; var yPos = 0;

(文档)$。就绪(函数(){     $( “项目”)。拖动({         遏制:'#house_wall1',

    drag: function(){
        offset = $(this).offset();
        xPos = offset.left;
        yPos = offset.top;
        $('#posX').text('x: ' + xPos);
        $('#posY').text('y: ' + yPos);
    },

    // Find original position of dragged image.
    start: function(event, ui) {
        // Show start dragged position of image.
        var Startpos = $(this).position();
        $("div#start").text("START: \nLeft: "+ Startpos.left + "\nTop: " + Startpos.top);
    },

    // Find position where image is dropped.
    stop: function(event, ui) {
        // Show dropped position.
        var Stoppos = $(this).position();
        $("div#stop").text("STOP: \nLeft: "+ Stoppos.left + "\nTop: " + Stoppos.top);
    }
});

});

除此之外,我有一个按下按钮运行的AJAX脚本。 <button onclick="houseAjax()">Save positions</button> 该脚本如下所示:

function houseAjax(){
var xmlhttp;
if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
}
else {// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}

xmlhttp.onreadystatechange=function(){
    if (xmlhttp.readyState==4 && xmlhttp.status==200){
        $.post("update_house.php",{newx: xPos, newy: yPos},function(result){
          });
    }
}
xmlhttp.open("POST","update_house.php", true);
xmlhttp.send();

} 最后我有update_house.php,它应该使用预准备语句将x和y值发送到数据库中。

  <?php
        require_once('includes/db_connect.php');

        $newX = $_POST['newx'];
    $newY = $_POST['newy'];

    /* Register a prepared statement */
    if ($stmt = $mysqli->prepare('UPDATE house_room1 SET x = ?, y = ? WHERE `user_id`=?')) { 

        /* Bind parametres */
        $stmt->bind_param('iii', $newX, $newY, $id);

                /* Insert the parameter values */
                $id = 1;

                /* Execute the query */
                $stmt->execute();

                /* Close statement */
                $stmt->close();

            } else {
                /* Something went wrong */
                echo 'Something went terrible wrong'     . $mysqli->error;
            }
?>

我的问题是x和y值不会以某种方式正确地从JS解析为PHP。这些值不会进入数据库,并且在运行所有这些代码时,数据库中没有任何反应。我知道这个问题可能出现在上面代码的各个方面,我希望有人可以找到我没有正确理解的东西。提前谢谢。

更新: 现在,当按下按钮时,数据库可以将0插入到数据库中,现在问题似乎是跟踪未正确解析到数据库的x和y位置的javascript变量。感谢

3 个答案:

答案 0 :(得分:1)

试试这个,你错过了在变量名前添加$

 $newX = $_POST['newx'];
 $newY = $_POST['newy'];

if ($stmt = $mysqli->prepare('UPDATE house_room1 SET x = '".$newX."', y =  '".$newY."' WHERE `user_id`=?')) { 

此外,$.post网址应为"update_house.php"而不是"update_house.php.asp"

答案 1 :(得分:1)

我会检查你的PHP代码,即你定义变量和构建查询的方式:

$newX = $_POST['newx'];
$newY = $_POST['newy'];

/* Register a prepared statement */
if ($stmt = $mysqli->prepare('UPDATE house_room1 SET x = ?, y = ? WHERE `user_id`=?')) { 

    /* Bind parametres */
    $stmt->bind_param('ddi', $newX, $newY, $id);
    ...

答案 2 :(得分:0)

试试这段代码

$newX = $_POST['newx'];
$newY = $_POST['newy'];

if ($stmt = $mysqli->prepare('UPDATE house_room1 SET x = ?, y = ? WHERE `user_id`=?')) { 

/* Bind parametres */
$stmt->bind_param(1, $newX);
$stmt->bind_param(2, $newY);
$stmt->bind_param(3, $id);