<div class="rotary"> <div class="rotaryArrow" id="rotaryArrow"></div> <div class="list"> <ul id="li"> </ul> </div> <script type="text/javascript"> $.ajax({ type:'POST', url:'test1.php', data:['li'], dataType: 'json', cache:false, error: function(){ alert('出错了!'); return false; }, success: function(json){ var list = ''; for(var o in json){ list += json[o]; } $("#li").html(list); } });
要求返回格式是json,并且需求只返回数组,再由html里面拼成<li>
<?php $servername = "localhost"; $username = "root"; $password = "123"; $dbname = "list"; $conn = new mysqli($servername, $username,$password,$dbname); if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $sql = "SELECT num FROM tele"; $result = $conn->query($sql); if ($result->num_rows > 0) { $li=''; while($row = $result->fetch_assoc()) { $li .= '<li>'. $row["num"].'</li>'; } } else { echo "0 results"; } $conn->close(); echo json_encode($li); ?>
dudu大神快来就我