<?php header("Content-type: text/html; charset=utf-8"); $link=mysql_connect("localhost","root",""); mysql_select_db("list"); $result = mysql_query("SELECT number FROM telephone"); while ($row = mysql_fetch_array($result)) { echo ('<li>'.$row["number"].'</li>'); } mysql_close($link); ?>
<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); } }); </script>
PHP中将
while ($row = mysql_fetch_array($result)) { echo ('<li>'.$row["number"].'</li>'); }
改为
$html = ''; while ($row = mysql_fetch_array($result)) { $html .= '<li>'.$row["number"].'</li>'; } echo $html;
js代码改为
$.ajax({ type:'POST', url:'test1.php', data:'li', dataType: 'text', cache: false, error: function(){ alert('出错了!'); return false; }, success: function(data){ $("#li").html(data); } });
dudu大神 这个要用mysqlLi写怎么写啊
@L娘: 是mysqli吗?
@dudu: 是的~! 我是在是不会写了 dudu大神!
@L娘: 参考:mysqli操作数据库
@dudu: dudu大神 我写出来了
<?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 number"; $result = $conn->query($sql); if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { echo "<li>" . $row["num"]. "</li>"; } } else { echo "0 results"; } $conn->close(); ?>
dudu大神 那我应该怎么传回我的js里面啊
@L娘: 不要直接echo,先拼接字符串,最后echo,参考回答中的代码
继续受折磨把!
你的<>要转义。 < = <还是什么,我大已忘却。百度一手吧!
var result = source.Replace(" ", " ");//空格
result = result.Replace("<", "<");
result = result.Replace(">", ">");
result = result.Replace("\"", """);
result = result.Replace("\t", " ");//制表符
result = result.Replace("\r\n", "<br/>");//换行