好的,這裏是一個小例子,你如何做到這一點。當您使用JSON時,它實際上很簡單。使用PHP中的json_encode()
,您可以輕鬆地將數組轉換爲JSON,通過AJAX接收數據並使用每個循環處理數據。
下面是代碼,您可以使用testings: HTML:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>AJAX JSON Example</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script>
$(function(){
$.getJSON('ajax.php',function(data){
$.each(data,function(index,value){
$('body').append('Name: ' + value.name + ' | City: ' + value.city + '<br />');
})
});
});
</script>
</head>
<body>
</body>
</html>
PHP:
<?
// I was too lazy to set up a DB for this example.
// You should know how to process database data in PHP
/*
$sql = "SELECT `id`,`name`,`city` FROM `useres`";
$result = $db->query($sql);
while($array = $db->assoc($result))
{
$json['id']['name'] = $array['name'];
$json['id']['city'] = $array['city'];
}
*/
// This part is only the example data which you should have received from the DB
$json[1]['name'] = 'Test Tester';
$json[1]['city'] = 'Test Village';
$json[2]['name'] = 'Claud Atlas';
$json[2]['city'] = 'Haven';
// echo the array as JSON
echo json_encode($json);
?>
這裏有從上面的代碼中的活生生的例子:https://work.walter-it.de/test/ajax/
當你在客戶端循環數據時 - 爲你循環的每個元素創建一個容器/行。在循環結束時,附加到頁面上的容器。 – tymeJV
顯示您遇到問題的代碼。 – trincot
我能想象的唯一答案就是指向教程的指針,它是SO的Off Topic。[一個最小的,完整的和可驗證的例子](http://stackoverflow.com/help/mcve)你嘗試過的東西是唯一能夠從一個近距離投票候選人中提出這個問題的東西 – RiggsFolly