2017-07-26 79 views
-1

即时通讯我的脚本我想从ajax.php读取并显示它们,并在我的ajax.php我想从数据库中获取。我的问题是从数据库取不工作!从ajax数据库取回

echo 
       '<?php while ($row = mysqli_fetch_array($products)):?> 
       <div class="item-in-cart clearfix"> 
         <div class="image"> 
          <img src="images/<?php echo $row['id'] ?>" width="124" height="124" alt="cart item" /> 
         </div> 
         <div class="desc"> 
          <strong> 
           <a href="product.html">s</a> 
          </strong> 
          <span class="light-clr qty"> 
           <a href="#" class="icon-remove-sign" title="Remove Item"></a> 
          </span> 
         </div> 
         <div class="price"> 
          <strong></strong> 
         </div> 
       </div>'; 
       <?php endwhile;?> 


    $(document).ready(function(){ 
     $(".addCart").click(function(){ 
      var id = $(this).val(); 
      $.ajax({ 
       type:'post', 
       url:'ajax.php', 
       data :{ 
        id : 'id', 
       }, 
       success : function (response) { 
       $('#cart').html(response); 
       } 
      }); 

     }); 
    }); 
+0

定义“不起作用”。还有,你为什么在浏览器中回显PHP代码? –

+0

@SamiKuhmonen,因为我想给我一个回应,我在ajax中显示它们到我的索引页面 – sinak

+0

你的代码中有语法错误。 – roberto06

回答

1

在您的AJAX的返回循环中,将所有结果放在一个连接字符串中放入一个变量中。然后回显该变量作为ajax中的返回。

确保有一个ID“购物车”

ajax.php

<?php 
$return = ""; 
while($row = mysqli_fetch_assoc($products)): 
$id = $row['id']; 
$return .= "<div class='item-in-cart clearfix'> 
    <div class='image'> 
     <img src='images/$id' width='124' height='124' alt='cart item' /> 
    </div> 
    <div class='desc'> 
     <strong> 
      <a href='product.html'>s</a> 
     </strong> 
     <span class='light-clr qty'> 
      <a href='#' class='icon-remove-sign' title='Remove Item'></a> 
     </span> 
    </div> 
    <div class='price'> 
     <strong></strong> 
    </div> 
</div>"; 
endwhile; 

echo $return; 
?> 

然后HTML数据的肿块会推并插入车ID标签里面,但也是一个标签在调用ajax之前或者在它插入之前,首先清空购物车ID

$('#cart').html(''); 
$('#cart').html(response);