i am working on a single page website, where there is grid of products, visters will simple add that product and press check out to order products,
right there i am facing issue, the products are of dryfruits, which contain 0.5,1 and 2Kg with different rates, by using ajax i am trying to display rate of product based on quantity selected on the spot,but what actually actually happens is that only i am able to get value of first product rest are not displayed on click
here is the code
php and mysql code
<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 Code
<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 code
<?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 }?>
I will be very thankyou if you guyz highlight where i am missing