I want to Create a Shoping Cart on PHp,
The Code is simple, When the customer fill the QTY and click button Add to cart, the code will save the PRoduct ID and Qty to a Cart Table. But the problem is that Form inside the Looping. And How Can I Get ID and Qty only from The button that Customer Click.
The Program look like this
And The Script Like This
<?php
if(isset($_POST[ADD]))
{
$qty = $_POST[QTY];
$harga = $_POST[HARGA_ASLI];
$id = $_POST[ID];
print_r($_POST);
}
$kolom = 3;
$sql = "SELECT *,FORMAT(harga,0)AS harga_digit FROM item";
$hasil = mysql_query($sql);
echo "<form method=POST action=index.php>";
echo "<table>
<tr>";
$i = 0;
while($data=mysql_fetch_array($hasil))
{
if($i >= $kolom)
{
echo "</tr><tr>";
$i = 0;
}
$i++;
echo "<td align='center'><br><a href='detailBarang.php?ID=$data[ID]'><img src='$data[img]' width='200' height='150'/><br>$data[nama_produk]</a><br>
Rp. $data[harga_digit]<br>
<input type='submit' name='ADD' id='ADD' value='Add to Cart'>
<input type='text' name='QTY' id='QTY' placeholder='Qty' /><br>
<input type='hidden' name='HARGA_ASLI' id='HARGA_ASLI' value='$data[harga]' /><br>
<input type='hidden' name='ID' id='ID' value='$data[ID]' />
<br></td>";
}//end of while
echo "<tr></table>";
echo "</form>";
?>
If i fill the Qty and click Add to Cart, only the last Item can Post The Data.
How To Post The data Only for Customer Choose?
Im very Appreciated Your Answer.
Thanks