dongnu4254 2014-02-19 17:52 采纳率: 100%
浏览 16
已采纳

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

I am making a site with images that can be dragged around. I then have a Javascript code which detects where on the page the user is dragging them by x and y coordinates. For that, I have this code: (house_position.js)

    var offset = 0;

var xPos = 0; var yPos = 0;

$(document).ready(function(){ $(".item").draggable({ containment: '#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: 
Left: "+ Startpos.left + "
Top: " + Startpos.top);
    },

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

});

Besides that, I have a AJAX script that is run by a press on a button. <button onclick="houseAjax()">Save positions</button> That script looks like this:

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

} And finally I have update_house.php that is supposed to send the x and y values into a database using prepared statements.

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

My problem is that the x and y values doesn't get parsed from JS to PHP correctly somehow. The values does not get into the database and when all this code is run, nothing happens in the database. I know this problem can be in every aspects of the codes above and I hope that it is possible for someone to locate the things that I have not understood correctly. Thanks in advance.

UPDATE: The database can now insert 0 into the database when the button is pressed, the problem now seems to be something about the javascript variable that tracks the x and y position that doesn't get parsed to the database correctly. Thanks

  • 写回答

3条回答 默认 最新

  • douchen7366 2014-02-19 18:00
    关注

    I would check your PHP code, namely the way you are defining the variables and building the query:

    $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条)

报告相同问题?