local-host 2017-07-26 09:45 采纳率: 100%
浏览 6

在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!

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);
                }
            });

        });
    });
  • 写回答

1条回答 默认 最新

  • weixin_33701294 2017-07-26 09:57
    关注

    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"

    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 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

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

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘