2017-07-26 74 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);