只显示网格的第一个产品价格的Ajax没有显示

时间:2018-06-14 09:18:34

标签: php mysql ajax product

我正在一个单页网站上工作,那里有产品网格, visters将简单地添加该产品并按下签出订购产品,

就在那里我面临问题,产品是干果,含有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 }?>

如果你突出我缺少的地方,我将非常感谢你

0 个答案:

没有答案