Jurgen Cuschieri
Jurgen Cuschieri

Reputation: 736

Form / Submit Button in PHP

This .php file outputs items from an XML file onscreen and gives the user the chance to click on the add to cart submit button which is displayed after each item. The idea is that the ID, Name etc are further used in a shopping cart system, but I am still in the starting stages of the shopping cart. (This is for an assignment)

But the problem is that this method does not work since when all the iterations are done, irrespective to which button is clicked, the computer will always refer to the values stored in the variables in the last iteration of the for loop.

I want to write some code in order to be able to have a UNIQUE button for each ID, and when this button is pressed, the corresponding information to that item is put through thi: action="add.php"

Any help would deeply be appreciated because I am stuck, and I have no idea

<?php
$doc = new DOMDocument();
$doc->load('menu.xml' );

$Wraps = $doc->getElementsByTagName ("Wraps");

foreach ($Wraps as  $w) {
$wrapsid = $w->getElementsByTagName("id")->item(0)->nodeValue;
$wrapsname = $w->getElementsByTagName("Type")->item(0)->nodeValue;
$wrapsprice = $w->getElementsByTagName("Price")->item(0)->nodeValue;

echo "Wraps ID: $wrapsid <br> Wraps Name: $wrapsname <br> Wraps Price:  $wrapsprice";

?>
<form action="add.php" method="post">
                    <td class='view_td' width='10%'><center>
                   <INPUT TYPE=HIDDEN NAME='ID' value='{$wrapsid}'/>
                   <input type='submit' value='Add to cart' /></center></td><br>
</form>
    <?php
    }
    ?>

Upvotes: 0

Views: 678

Answers (2)

MadDokMike
MadDokMike

Reputation: 182

you can't put form tags around a TD it has to go around the whole table, either use divs and style it to replicate a table layout using CSS or use GET as you are only passing the ID back to the server eg. use an anchor instead of your input

Upvotes: 2

mojtaba
mojtaba

Reputation: 339

To identify the submitted form, you can use:

•A hidden input field.
•The name or value of the submit button.
The name of the form is not sent to the server as part of the POST data.
more information

Upvotes: 1

Related Questions