php - fetch from database in ajax -
im script want read ajax.php , show them , in ajax.php want fetch database . problem fetching database doesnt work!
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); } }); }); });
on loop of return of ajax, put result in 1 concatenated string inside variable. echo variable serve return in ajax.
make sure there tags id "cart"
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; ?> then lump of html data push , insert inside cart id tags, upon calling ajax, or before insert, empty cart id first
$('#cart').html(''); $('#cart').html(response);
Comments
Post a Comment