将JS值解析为MySQL DB

时间:2014-02-19 14:54:11

标签: javascript php mysql

我有一个脚本,通过在Javascript中更新变量来跟踪某些图像位置。但是我需要将该值解析为DB,以便在用户下次登录时使对象保持相同的位置。我的JS代码如下:(house_positions.js)

    $(document).ready(function(){
$(".item").draggable({
containment: '#house_wall1',

drag: function(){
        var offset = $(this).offset();
        var xPos = offset.left;
        var 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);

}
});
});

然后我有MySQL数据库调用:

<?php
        require_once('x'); // don't mind the connection

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

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

                /* Bind parametres */
                $stmt->bind_param('i', $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;
            }
?>

如您所见,我尝试将Javascript中的“xPos”变量插入到PHP中的MySQL语句中。我不相信这会起作用,但很好。谁能告诉我这是怎么回事?提前谢谢。

更新: 我的Ajax代码:

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.asp",{window.newx: xPos, window.newy: yPos},function(result){

                  });
                }
              }
            xmlhttp.open("POST","update_house.php", true);
            xmlhttp.send();
            }

1 个答案:

答案 0 :(得分:0)

你做错了,当调用更新你的数据库的php函数时,你必须将你的参数从javascript代码传输到你的PHP。和你之前一样,你的php不会知道它。

Java脚本示例:

$.post("http://www.pbpixels.com/x/update_house.php",{newx:xPos, newy: yPos},function(result){
    //data is updated
  });

无论你在服务器端php页面中捕获ajax请求,只需从调用中获取它:

PHP:

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

现在调用更新函数:

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

无论如何,我建议您通过some reading

来扩展您的Ajax知识

祝你好运