php - 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 }?>
如果你能突出我失踪的地方,我将非常感谢
解决方案
推荐阅读
- java - 如何将字体样式标签添加到 JavaFX 中的文本?
- c# - C#:单选按钮没有定义.IsChecked?
- azure - 无法使用存储帐户参与者权限在 HDInsight 和 ADLS Gen 2 上打开 pyspark
- java - 以下代码是否可以检查对象数组的内容是否彼此相等?
- java - 如何使用 JNI android 获取应用程序构建配置(调试或发布)?
- sql-server - 具有 NVARCHAR 数据类型的 SQL RegEx 语法
- java - Xpath - 使用带有部分文本的多个属性的复合语句
- reactjs - 我怎样才能让这个 React/Redux 应用程序正确呈现?
- javascript - 从节点加密生成的随机值中消除破折号?
- angularjs - ng-view '应用程序启动后超出最大调用堆栈大小