user3287771
user3287771

Reputation: 113

PHP, JS, AJAX can't send value to 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: \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);
    }
});

});

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

Upvotes: 0

Views: 128

Answers (3)

Nishant Solanki
Nishant Solanki

Reputation: 2128

try this code

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

Upvotes: 0

LNendza
LNendza

Reputation: 1400

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

Upvotes: 1

Krish R
Krish R

Reputation: 22711

Try this, You have missed to add $ in front of variable name

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

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

Also, $.post url should be "update_house.php" instead of "update_house.php.asp"

Upvotes: 1

Related Questions