我有一个名为index.html的页面和一个名为topics.php的php文件。topics.php如下所示:
<?php
$topics = array("Technology", "Badminton", "Languages");
echo(json_encode($topics));
?>
我希望在 index.html 上列出这些主题(而且由于index.html页面还将有一个名为Submit/Create a topic的表单,因此$topics数组将被更新为添加项,并且希望index.html上的列表能够自动更新。这应如下所示:
- Technology
- Badminton
- Languages
- Writing
- Art
在index.html网页上。(假设后来通过表单添加了写作和艺术)
我必须使用ajax/jquery,最好以json格式进行数据传输。怎么做呢?
发布于 2016-12-25 04:21:41
您可以简单地使用循环遍历数组,并在html中与数组元素一起打印未排序的列表。
<?php
$topics = array("Technology", "Badminton", "Languages");
?>
<html>
<body>
<ul>
<?php
foreach($topics as $v)
{
echo '<li>'.$v.'</li>';
}
?>
</ul>
</body>
</html>
这是动态打印列表元素的最简单解决方案。
https://stackoverflow.com/questions/41320874
复制