ajax只显示第一个产品的价格,其余的网格不显示

8nuwlpux  于 2021-06-21  发布在  Mysql
关注(0)|答案(0)|浏览(282)

我在一个单页网站上工作,那里有产品网格,访问者会简单地添加该产品,然后按check out来订购产品,
就在这里我面临的问题,产品是干果,其中含有0.5,1和2公斤不同的比率,通过使用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 }?>

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

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题