0

I'm trying to create a checkout page in PHP. The page that lists the items is called shop.php. On this page, I display a number items to be sold with a checkbox beside each item. I'm pulling the item name and price information from the database. Here's my main code.

$row = mysql_fetch_array($query);
while($row) {
     echo "<TR>";
     echo "<TD><B>$row[Name] </B></TD>";
     echo "<TD><B>"."$"."$row[Price] </B></TD>";
     echo "<TD><input type=checkbox name=foods[] value='$row[Price]' /></TD>";
         echo "</TR>";
$row = mysql_fetch_array($query);
}

<input type="submit" name="buy" value="Buy Now" />

So I am setting the name of the checkboxes to foods[] which'll be an array that'll contain values of all checkboxes submitted. When the user clicks on the "Buy Now" button, they'll be redicted to the cart.php page, that'll display the name and price of the item they've purchased. I know that I can output the prices by looping through the $items array. The problem I have is that I don't understand how would I display the item names. I thoght of doing the following:

 $row = mysql_fetch_array($query);
 while($row) {
   echo "<TR>";
   echo "<TD><B>$row[Name] </B></TD>";
   echo "<TD><B>"."$"."$row[Price] </B></TD>";
   echo "<TD><input type=checkbox name='$row[Name]' value='$row[Price]' /></TD>";
    echo "</TR>";
$row = mysql_fetch_array($query);
}

This time, both the name and the price of the items are included in the checkbox as its name and value. But again, the problem is how would I display them on the cart.php page without hard coding. What I means is that how would I display the item name and its price without doing something like this:

(Assume that one of the items name is Burger which was populated as the name of the checkbox.)

if (isset($_POST['Burger'])){
echo "<td>$_POST['Burger']</td>";
}

I'd appreciate any suggestions/tips on this question. Thank you.

2 Answers 2

1

If you name the checkboxes like:

<input type='checkbox' name='foods[{$row['Name']}]' value='{$row['Price']}' />

You can access the array $_POST['foods'] and the key/value will be Name/Price. So you can do:

foreach($_POST['foods'] as $name => $price) {
    echo "<tr><td>$name</td><td>$price</td></tr>";
}
Sign up to request clarification or add additional context in comments.

Comments

1

Set name to each checkbox as foods[$row[name]]. You should have following code:

$row = mysql_fetch_array($query);
while($row) {
     echo "<TR>";
     echo "<TD><B>$row[Name] </B></TD>";
     echo "<TD><B>"."$"."$row[Price] </B></TD>";
     echo "<TD><input type=checkbox name='foods[{$row[Name]}]' value='$row[Price]' /></TD>";
         echo "</TR>";
$row = mysql_fetch_array($query);
}

<input type="submit" name="buy" value="Buy Now" />

On POST you will have associated array of item names and prices.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.