0
我做一個Ajax調用,一切工作正常,我現在用的腳本是:取回從PHP頁面的響應,並用它在JavaScript調用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的新手,所以我不太瞭解它。 任何幫助表示讚賞。謝謝。
你沒有捕獲ajax響應?你有一個成功的功能,但評論它。你的代碼片段做的比你所問的要多得多 - 保持沉默,直到它只有你所關心的部分 - 這將幫助你,它會幫助別人回答。 – DrLivingston
在ajax文件中嘗試'echoing something',然後在'ajax success'中檢測'alert(result)'..檢查它返回的結果 –
我需要將返回的值存儲在一個javascript變量中..... how我是否這樣做? @DrLivingston –