根据php对ajax请求的响应制作动态html表

时间:2016-12-13 17:27:22

标签: javascript jquery html ajax

我有一个页面,它发送请求到PHP获取一些数据,并响应php的请求返回我这样的事情: -

[{"postID":"1","0":"1","userID":"3","1":"3","imagePath":"images\/31481440272.jpg","2":"images\/3-1481440272.jpg","postDate":"11 December 2016","3":"11 December 2016","postDescription":"trying","4":":D trying"}]

我想要的是在特定div中创建一个动态html表

<div id="dataWillBeShownHere"></div>

如果记录超过1,我该怎么办。我应该为每个循环使用多个记录。

我真的很困惑。不知怎的,我知道解决方案,但无法实现。

我希望你能理解我的问题。

1 个答案:

答案 0 :(得分:1)

您可以使用简单的javascript和html执行此操作:

<script type="text/javascript">
var records = [];
// This data could be retrieved from an ajax $.post
var data = {"postID":"1","0":"1","userID":"3","1":"3","imagePath":"images\/31481440272.jpg","2":"images\/3-1481440272.jpg","postDate":"11 December 2016","3":"11 December 2016","postDescription":"trying","4":":D trying"};

// Add our data to the records array
records.push(data);

// Get how many record we have
var record_count = records.length;

// Parse data to html tables
for (var i = records.length - 1; i >= 0; i--) {
    var html = "";

    // make your html additions here
    html += "<div> ID: " + records[i]["postID"] + "</div>";
    html += "<div> Date: " + records[i]["postDate"] + "</div>";
    // html += ;
    // html += ;

    // Now use jquery selector to write to the div
    $('#data-table').html(html);
};
</script>


<div id="data-table"></div>

检查我的JSFiddle