I'm displaying products on my index page by fetching products from database and then displaying them with the help of foreach loop in php. The problem is that, I want to get only one single product id and quantity when the form is submitted on the criteria that which product user has selected to add to the cart.
I'm only getting the first product order_code all the time when I click on add to cart button.
<script type="text/javascript">
$(function() {
$("form.cart_form").submit(function() {
var title = "Your Shopping Cart";
var orderCode = $("input[name=order_code]", this).val();
var quantity = $("input[name=quantity]", this).val();
var url = "cart_action.php?order_code=" + orderCode + "&quantity=" + quantity + "&TB_iframe=true&height=400&width=780";
tb_show(title, url, false);
return false;
});
});
</script>
The following is the HTML of that page.
<div id="container">
<h1>Shopping Cart Demo</h1>
<form class="cart_form" action="cart_action.php" method="get">
<?php
$count = 0;
$i = 1;
foreach($products as $product):
if ($count % 2 == 0) {
echo "<div class='images'>";
}
echo "<img src='$product->pro_img'/>";
?>
<input type="hidden" name="order_code" value="<?php echo $product->pid; ?>" />
<label><?php print $product->name; ?> <input class="center" type="text" name="quantity" value="1" size="3" ?></label>
<label>Price: $<?php echo $product->price; ?></label>
<input type="submit" name="submit" value="Add to cart" />
<?php
if ($i % 2 == 0) {
echo "</div>";
} $count++;
$i++;
endforeach;
?>
</form>
</div>