简体   繁体   中英

Xml reply from PHP to post of jquery Ajax

i have a javascript code that requests a php page to provide it with list of names that are currently online and update a Table, but i have a problem sending it back in form of an array, someone told me that this is usually done using XML, but i dont know how to start.

javascript Post method:-

$.post( "updateTable.php", POSTdata,
  function( data ) {
     $("#mytable").last().append('<tr><td>'+data+'</td></tr>');
   }
 );

the php file:-

    include("connect.php");
    $query1 = "SELECT * FROM formtable";

    $result_id = mysql_query($query1, $global_dbh)
    or die ("display_db_query:" . mysql_error());

    while ($table_array = mysql_fetch_object ($result_id))
    {
        $rows[] = $table_array; 

    }   
    foreach ($rows as $temp ) { 
        if ($temp->isOnline==1) 
            $newRow[] =  $temp->name;
    }
    echo "$newRow";

mysql_close($global_dbh);

Please excuse any syntax or semantics in my code, i am a beginner. How can i populate my table using ajax callback function, and in what form the data will arrive there, and how can i use xml to help me. Many thanks in advance.

A quick example of json:

var table = $("#mytable").last();
$.ajax({
    type: 'post',
    url: "updateTable.php",
    dataType: 'json',
    data: POSTdata,
    success: function(data){
        jQuery.each(data, function(i, row){
            //console.log(row);
            table.append('<tr><td>'+row.name+'</td></tr>');
        });
    }
});

and in php file, instead of :

echo "$newRow";

replace with:

echo json_encode($newRow);

That's it!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM