您的位置 >>> 星想互联 >>> 编程技术 >>> PHP基础
PHP+AJAX异步加载新闻的新闻首页
点击数:1921  发布时间2017-10-10 22:28:46

index.php页面:

<?php header("Content-type: text/html; charset=gb2312");?>

<style>
*{font-size:12px;padding:0;margin:0;}
ul,li{list-style:none}
div{width:30%;float:left;margin-right:3.333%}
</style>
<script src="jquery-1.9.1.min.js"></script>
<script>
$.ajax({
 type:"GET",
 url:"view.php",
 data:{typeid:3},
 beforeSend: function(){$("#con1").html("正在加载中......")},
 success:function(data){
  $("#con1").html(data);
 }
 
})

$.ajax({
 type:"GET",
 url:"view.php",
 data:{typeid:7},
 beforeSend: function(){$("#con1").html("正在加载中......")},
 success:function(data){
  $("#con2").html(data);
 }
 
})

$.ajax({
 type:"GET",
 url:"view.php",
 data:{typeid:5},
 beforeSend: function(){$("#con1").html("正在加载中......")},
 success:function(data){
  $("#con3").html(data);
 }
 
})
</script>
<div>
<ul id="con1"></ul>
</div>

<div>
<ul id="con2"></ul>
</div>

<div>
<ul id="con3"></ul>
</div>


view.php页面:

<?php

header("Content-type: text/html; charset=gb2312");

$mypdo=new pdo("mysql:host=localhost;dbname=news","root","");
$typeid=$_GET["typeid"];
$sql="selects * from news where typeid=$typeid limit 0,10";
$mypdo->query("set names gb2312");
$stmt=$mypdo->query($sql);


$rows=$stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($rows as $rs){
 echo "<li>".$rs['title'].$rs['fdate']."</li>";
}
?>

来源:咸宁网站建设