首页 > 解决方案 > Ajax 仅显示第一个产品价格其余网格未显示

问题描述

我正在一个单页网站上工作,其中有产品网格,访问者将简单地添加该产品并按结帐以订购产品,

就在那里我面临问题,产品是干果,包含 0.5,1 和 2Kg 不同的费率,通过使用 ajax 我试图根据现场选择的数量显示产品的费率,但实际发生的是只有我能够获得第一个产品的价值,其余部分不会在点击时显示

这是代码

php和mysql代码

  <div class="row">
                <div class="container">
                <?php
                    $get_products = "select * from products";

                    $run_products = mysqli_query($connection,$get_products);

                    while ($rows  = mysqli_fetch_assoc($run_products))
                    {

                        $product_id = $rows['product_id'];
                        $product_name = $rows['product_name'];

                        $product_img = $rows['product_image'];

                 ?>
                    <div style="border: 2px solid green; height: 350px; "  class="col-lg-4">
                            <form method="get" >

                        <input type="hidden" value="<?php echo $product_name ; ?>" name="product_name">    
                            <strong style="font-size: 24px; color: brown"><?php echo $product_name ; ?></strong>
                            <img class="thumbnail" style="border: 1px solid gray" src="images/<?php echo $product_img; ?>" width="100px" height="100px" alt="54">
                        <div class="form-group">
                            <label class="control-label">Qty</label>
                            <select onchange="change_country()" name="quant" id="quant" style="width: 100px;" class="form-control">
                                <option selected="">Select</option>
                                <?php

                                    $sql = "select * from quantities where product_id = 1";
                                    $run_sql = mysqli_query($connection,$sql);
                                    while ($row = mysqli_fetch_assoc($run_sql))
                                    {
                                        $quantity_id = $row['quantity_id'];
                                        $product_id = $row['product_id'];
                                        $quantity = $row['quantity'];
                                        $quantity_price = $row['quantity_price'];
                                 ?>
                                <option value="<?php echo $quantity;  ?>"><?php echo $quantity; ?></option>
                                <?php } ?>
                            </select>
                            <input type="hidden" value="<?php echo $product_id; ?>" name="id">
                        </div>
                        <div id='state' class="form-group">

                        </div>
                        <div class="form-group">
                            <input class="btn btn-warning" type="submit" value="Add item" name="add_item">
                        </div>
                    </div>
                        </form>
              <?php      }?> 

</div></div></div></div></div>

/// Ajax 代码

<script type="text/javascript">
    function change_country()
    {
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.open("GET","ajax.php?country="+document.getElementById("quant").value,false);
        xmlhttp.send(null);
        document.getElementById("state").innerHTML = xmlhttp.responseText;  
    }
</script>

Ajax.php 代码

<?php

        $db = new mysqli('localhost','root','','orderdryfruits');

        if(!$db)
        {
            die("connection".mysqli_error($db));
        }       

        $country  = $_GET['country'];

    if($country != "")
{

        $res = mysqli_query($db,"select * from quantities where quantity = $country");

        $row = mysqli_fetch_assoc($res);
?>
<label style="margin-right: 2px;">Price</label><input style="width: 100px; type="text" name="Price" value="<?php echo $row['quantity_price'] ?>"> 

<?php }?>

如果你能突出我失踪的地方,我将非常感谢

标签: phpmysqlajaxproduct

解决方案


推荐阅读