2017-02-14 20 views
0

我能夠使用json完成數據顯示但是如何將所有數據庫都打印到div或table?爲json發送的數據創建循環

JS

$(document).ready(function() { 
    $.ajax({ 
    type : 'POST', 
    url : 'server.php', 
    dataType:"json", 
    success : function (data) { 

     alert(data[1].name); 

    } 
    }); 
}); 

PHP

<?php 
    $db = new PDO('mysql:host=localhost;dbname=Contact', 'root', ''); 
    $statement=$db->prepare("SELECT * FROM myfeilds"); 
    $statement->execute(); 
    $results=$statement->fetchAll(PDO::FETCH_ASSOC); 
    $json=json_encode($results); 
    echo $json; 

?> 
+1

你可以使用一個表庫(如datatables) –

+0

與一個數據,並使用jquery來生成新的DOM –

回答

0

創建一個HTML文件與至少一個DIV:

<html> 
    <div id="test"></div> 
</html> 

JS

$(document).ready(function() { 
    $.ajax({ 
    type : 'POST', 
    url : 'server.php', 
    dataType:"json", 
    success : function (data) { 
     for(var y=0; y< data.length; y++){ 
     $("#test").append(data[y].name + " "); 
     } 
    } 
    }); 
});