我正在进行ajax调用,一切正常,我正在使用的脚本是:
$(document).ready(function() {
$('.sortable').sortable({
stop: function(event, ui) {
$(ui.item).effect("highlight");
//getting the id of the list element being moved and then
//we'll send it to the db for its id validation and
//updating the db upon drag and drop
var id = $(ui.item).attr("id");
//alert(id);
var pos = ui.item.prevAll().length;//prevAll().length
var position = ++pos;
//alert("Moved to position: " + position );//+ "from: " + id);
//var x = ui.offset.left;
//var y = ui.offset.top;
//alert("left: " + x + "top: " + y);
$.ajax({
url: "save.php",
data: {
position: position,
id: id
}
}); /*, success:function(result){alert(result);}*/
/* $.ajax({
url: "save.php",
data: {
x: x,
y: y
}
});*/ // data: {x: 'x', y: 'y'} ------->> for coordinates if needed*/
$(ui.item).effect("highlight");
}
});
})
我有一个PHP页面,它使用发送的值并在数据库中进行必要的更改。 我想知道如何从PHP脚本返回返回值,并通过javascript在调用页面上使用它。 PHP代码是:
$id1 = $_REQUEST['id'];
$pos1 = $_REQUEST['position'];//position where the element has been dragged to
$query = "select * from tab where id='$id1'";
$t = mysql_query($query) or die("nothing found in the database for the provided id. ".mysql_error());
if($t)
{
$r = mysql_fetch_array($t) or die("data could not be fetched from the DB ".mysql_error());
$r[0];//id of the dragged <li>element = $id1
$r[1];//its original position
$e = "select * from tab where original='$pos1'";
$y = mysql_query($e) or die("ERROR. ".mysql_error());
$u = mysql_fetch_array($y) or die("data could not be fetched from the DB for the replaced element. ".mysql_error());
$id2 = $u[0];//id of the place where the dragged <li> element was dropped
$u[1];//original position of the place where the dragged <li> element was dropped
$temp1 = $r[1];
$temp2 = $u[1];
$temp = $temp1;
$temp1 = $temp2;
$temp2 = $temp;
$up = "update tab set original='$temp1' where id='$id1'";
$q = mysql_query($up) or die("I query not done. ".mysql_error());
$up = "update tab set original='$temp2' where id='$id2'";
$q = mysql_query($up) or die("II query not done. ".mysql_error());
//echo"id1: " .$id1 ." id2: " .$id2 ." r[1]: " .$temp1 ." u[1]: " .$temp2;
请帮帮我。我是AJAX的新手,所以我对此并不了解。 任何帮助表示赞赏。感谢。
答案 0 :(得分:0)
在你的PHP上使用json_encode
//$array is value you want to return.
echo json_encode($array);
或者如果您不想从数组回显,则必须回显json格式。