Javascript PHP、JS、AJAX可以';t向数据库发送值
我正在制作一个可以拖动图像的网站。然后,我有一个Javascript代码,它可以检测用户在页面上以x和y坐标拖动它们的位置。为此,我有以下代码:(house_position.js) var-xPos=0; var-yPos=0 $(文档).ready(函数(){ $(“.item”).draggable({ 遏制:“#房屋(1)”Javascript PHP、JS、AJAX可以';t向数据库发送值,javascript,php,jquery,ajax,database,Javascript,Php,Jquery,Ajax,Database,我正在制作一个可以拖动图像的网站。然后,我有一个Javascript代码,它可以检测用户在页面上以x和y坐标拖动它们的位置。为此,我有以下代码:(house_position.js) var-xPos=0; var-yPos=0 $(文档).ready(函数(){ $(“.item”).draggable({ 遏制:“#房屋(1)” drag: function(){ offset = $(this).offset(); xPos = offset.lef
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);
}
});
}))
除此之外,我还有一个AJAX脚本,只需按一下按钮即可运行。Save positions
该脚本如下所示:
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();
}
最后,我更新了_house.php,它应该使用准备好的语句将x和y值发送到数据库中
<?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;
}
?>
我的问题是,x和y值没有从JS正确解析到PHP。这些值没有进入数据库,当所有这些代码运行时,数据库中什么也没有发生。我知道这个问题可能存在于上述代码的各个方面,我希望有人能找到我不了解的东西嘟嘟正确。提前谢谢
更新:
当按下按钮时,数据库现在可以将0插入数据库,现在的问题似乎是跟踪x和y位置的javascript变量没有正确解析到数据库中。多亏了尝试,您错过了在变量名称前面添加
$
$newX = $_POST['newx'];
$newY = $_POST['newy'];
if ($stmt = $mysqli->prepare('UPDATE house_room1 SET x = '".$newX."', y = '".$newY."' WHERE `user_id`=?')) {
另外,
$.post
url应该是的“update\u house.php”
而不是的“update\u house.php.asp”
我会检查您的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('ddi', $newX, $newY, $id);
...
试试这个代码
$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);
谢谢你的建议,但我担心这还不足以完全解决问题:)非常有趣,它现在将0发送到数据库,无论图像位置在哪里,这将继续。谢谢嗯,我写的答案很快。您可能希望在查询中使用浮点而不是整数。谢谢,我想使用整数,因为我的数据库保存整数,并且只能有实数。ThanksOk,下一步是查看哪些值被传递到PHP函数中!我已经更新了我的帖子,在帖子的底部,它准确地说出了我认为现在的问题所在。
$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);