欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Ajax实现不刷新取最新商品

程序员文章站 2023-11-05 20:21:04
话不多说,请看代码:

话不多说,请看代码:

<?php 
 $conn = mysql_connect('localhost','root','123456') or die('连接失败'); 
 mysql_select_db('ecshop'); 
 mysql_query('set names utf8'); 
 $attr = isset($_get['attr'])?$_get['attr']:'is_hot';  //由html 的ajax提交过来 然后根据它来取数据~ 
 $sql = 'select goods_name,goods_id,shop_price from goods where '.$attr.' = 1 limit 0,3'; 
 $rs = mysql_query($sql,$conn); 
 //var_dump($rs); 
 $goods = array(); 
 while($row = mysql_fetch_assoc($rs)){ 
  $goods[] = $row; 
 } 
 //print_r($goods); 
?> 
<table border='1'> //ajax 接收的就是 php输出内容 虽然它没echo 但也是输出在网页的内容 所以ajax 能接收~~!!! 
<tr><td>商品id</td><td>商品名称</td><td>商品价格</td></tr> 
<?php foreach($goods as $v){ ?> 
 <tr> 
  <td><?php echo $v['goods_id'];?></td> 
  <td><?php echo $v['goods_name'];?></td> 
  <td><?php echo $v['shop_price'];?></td> 
 </tr> 
<?php }?> 
</table> 

html 的内容

<script> 
 var xhr = new xmlhttprequest(); 
 function top3(attr){ 
  var url = 'goods.php?attr=' + attr; 
  xhr.open('get',url); 
  xhr.onreadystatechange = function (){ 
   if(xhr.readystate ==4){ 
    var dobj = document.getelementsbytagname('div')[0].innerhtml = xhr.responsetext; //把从php 接收的内容放入innerhtml 
   } 
  } 
  xhr.send(); 
 } 
</script> 
</head> 
<body> 
  <input type="button" value="最新商品" onclick="top3('is_new');"> 
  <input type="button" value="热卖商品" onclick="top3('is_hot');"> 
  <input type="button" value="精品商品" onclick="top3('is_best');"> 
  <div id="test"> 
  </div> 
</body> 

实例2:根据输入的id 获取商品信息 并修改

<?php 
$conn = mysql_connect('localhost','root','123456') or die('连接失败'); 
 mysql_select_db('ecshop'); 
 mysql_query('set names utf8'); 
 $id = isset($_get['id'])?$_get['id']:1; 
 if($id==''){ 
  $error['num'] = 1; 
  $error['msg']; 
 } 
 $sql = 'select goods_id,goods_name,shop_price,goods_number from goods where goods_id ='.$id; 
 $rs = mysql_query($sql); 
 if(!($goods = mysql_fetch_assoc($rs))){  //获取不到商品就返回false 
  echo '没有该商品!'; 
  exit; 
 } 
 echo json_encode($goods);     //把数组转成一个json 格式~~ 
?> 

html端的内容

<script> 
 var xhr = new xmlhttprequest(); 
 function modify(){ 
  var inputs = document.getelementsbytagname('input') 
  var gid = inputs[0].value; 
  var url = 'search.php?id='+ gid;
  xhr.open('get',url); 
  xhr.onreadystatechange = function (){ 
   if(xhr.readystate ==4){ 
    var data = eval('('+ xhr.responsetext +')') //把接收到的json 格式数据转成js的对象! 
    inputs[1].value = data.goods_name; 
    inputs[2].value = data.goods_number; 
    inputs[3].value = data.shop_price; 
   } 
  } 
  xhr.send(null) //经常漏写了~~~不写是发送不了请求的~~一定要写! 
 } 
</script> 
</head> 
<body> 
<h1>商品编辑</h1> 
  商品id:<input type="text" name="goods_id" onfocus="al()" onblur="modify();" /><br /> <span></span> 
  商品名称:<input type="text" name="goods_name" /><br /> 
  商品库存:<input type="text" name="goods_number" /><br /> 
  商品价格:<input type="text" name="shop_price" /><br /> 
  <input type="submit" value="修改" /> 
</body>

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!