簡體   English   中英

從PHP頁面獲取響應,並在ajax調用后在javascript中使用它

[英]get back a response from a PHP page and use it in javascript after an ajax call

我正在執行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的新手,所以我對此並不了解。 任何幫助表示贊賞。 謝謝。

在您的PHP上使用json_encode

//$array is value you want to return.
echo json_encode($array);

或者如果您不希望從數組中回顯,則必須回顯json格式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM