0

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

3 Answers 3

1

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);
    ...
Sign up to request clarification or add additional context in comments.

8 Comments

Very interesting, it now sends 0 to the database no matter where the image position is, this is going forward. Thanks!
Well, the answer I wrote up was quick. You probably want to use floats instead of integers in the query.
I want to use integers thanks since my database holds integers and there can only be real numbers. Thanks
Ok, so the next step is to see what values are being passed to your PHP function.
exactly! I have updated my post and in the bottom of it, it says exactly what I think is the problem now.
|
1

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"

1 Comment

Thanks for your suggestion but I'm afraid that wasn't enough to solve the problem completely :)
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.