sinak sinak - 3 years ago 94
jQuery Question

fetch from database in ajax

im my script i want to read from ajax.php and show them and in my ajax.php i want to fetch from database . my problem is fetching from database doesnt work!

'<?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 class="desc">
<a href="product.html">s</a>
<span class="light-clr qty">
<a href="#" class="icon-remove-sign" title="Remove Item"></a>
<div class="price">
<?php endwhile;?>

var id = $(this).val();
data :{
id : 'id',
success : function (response) {


Answer Source

On the loop of return of your AJAX, put all the result in one concatenated string inside a variable. then echo that variable serve as return in ajax.

make sure that there is an tags with an ID "cart"


$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 class='desc'>
            <a href='product.html'>s</a>
        <span class='light-clr qty'>
            <a href='#' class='icon-remove-sign' title='Remove Item'></a>
    <div class='price'>

echo $return;

then the lump of HTML data will be push and insert inside the cart ID tags, but also upon calling the ajax, or before it get insert, empty the cart id first

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download