开发者

Ajax : data to SQL

I'm a newbie in AJAX.

I looked for the solution in Google, but I didn't find a working solution.

So, I want to send data from a jquery's draggable div to sql via php.

index.html :

<html>
[...]
<body>
<myDiv>bla bla</div>

<script>
$( "myDiv" ).draggable({ stop: function() {
var position = $(this).position();
var xPos = $(this).css('left');

$.ajax({
      type: "post",
      url: "update.php",
      data: xPos,
      cache: false,
      }
}
});

</script>

</body>
</html>

update.php :

   <?php 
    require("db.php");

             $xpos = $_POST['xPos'];
             mysql_query("UPDATE item SET pos_x = '" . $xpos . "' WHERE ID = '". $post_ID."'");

        mysql_query($query) or d开发者_如何学Pythonie('Error, insert query failed');
    }
    ?>​

db.php :

  <?php
    $dbhost              = "**";
    $dbuser              = "**";
    $dbpass              = "**";
    $dbname              = "**";

    $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ("Error connecting to mysql");
    mysql_select_db($dbname);
    ?>​

Could someone tell me where I'm wrong?


You need to supply a named attribute for $_POST['xPos'] to work. Change this in your ajax call:

$.ajax({
      type: "post",
      url: "update.php",
      data: xPos, // Change this to --> data: 'xPos=' + xPos,
      cache: false,
      }
} // <-- get rid of this
});

Few errors in your update.php:

         // if xPos is not defined it could throw an error.
         // make sure you do if (isset($_POST['xPos'])) { [...below code here...] }
         $xpos = $_POST['xPos']; 

         // !! sql injection warning!!
         // change to mysql_real_escape_string($xpos),
         //           mysql_real_escape_string($post_ID) ..... etc.
         // also where is $post_ID defined? If this is a magic variable, naughty!
         // always use the global magic variables $_POST $_GET, etc.
         mysql_query("UPDATE item SET pos_x = '" . $xpos . "' WHERE ID = '". $post_ID."'");


    mysql_query($query) or die('Error, insert query failed');
}
?>​
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜