PHP Mysql Add to cart help

Hi there,
I am struggling with a bit of code and need a bit of help please…

I have information in a database that I want to display in a table, then allow users to choose a number of items and click ‘add to cart’.

It works fine if I include the ‘submit’ button within the loop but this means the user has to click ‘add to cart’ under every item… Ideally I want 1xSubmit button in the last column which would add all the selected items to cart. I have tried the code below but it only adds the last item of the loop into the cart.

Please help!

Thanks,
Dave

[php]

echo “

”;
echo “”;
while($row = mysql_fetch_array($result))  {
	
	echo "<form action='add-to-cart.php' method='post'>";
	
	echo "<td>".$row['id']."'><br>";
	echo "<input type='hidden' name='propornot' value=".$row['propornot']." />";
	echo "<input type='hidden' name='pid' value=".$row['id']." />";
	echo "<input type='radio' name='quantity' value='1'> 1 item<br>";
	echo "<input type='radio' name='quantity' value='2'> 2 items<br></td>";
}

  	echo "<td><input type='submit' name='submit' value='add to cart' class='fw' /></form></td>";
	echo "</table>";

[/php]

Choose items

Remove the form action from the while loop.
HTML will only process one form at a time.

When processing the form data from multiple items under the same name you will need to process them as arrays instead of individual data posts.

[php] echo “

”;
echo “”;

echo “”;

while($row = mysql_fetch_array($result))  {	
	echo "<td>".$row['id']."'><br>";
	echo "<input type='hidden' name='propornot[]' value=".$row['propornot']." />";
	echo "<input type='hidden' name='pid[]' value=".$row['id']." />";
	echo "<input type='radio' name='quantity[]' value='1'> 1 item<br>";
	echo "<input type='radio' name='quantity[]' value='2'> 2 items<br></td>";
}

  	echo "<td><input type='submit' name='submit' value='add to cart' class='fw' /></form></td>";
	echo "</table>";[/php]
Choose items

Additionally to the previous post you will need to use a foreach to do your insert. There is at least 1 code example on my website. Click on the link by my name.

Sponsor our Newsletter | Privacy Policy | Terms of Service