I have simplified the code to try figure this one out. Ive got a form/table that displays a list of items added to a users cart with buttons to remove each corresponding item however when any button is clicked the last item in the list is being passed to the $itemid variable instead of the selected item.
The $itemid is correctly displaying a diffrent itemid on each button in the table and ive got it to echo above the button as well but it just wont pass the selected itemid to the POST function when the button is clicked and i cant for the life of me figure out why.
The form code..
<form method="post" action="remove.php">
<table border="0" width="100%" class="main" cellpadding="4" cellspacing="0" align="center">
<tr> <td class="colhead" width="1%" align="left">Item</td> <td class="colhead" width="1%"> <center>Delete</center></td> </tr>
<?php while ($row = mysql_fetch_row($res)):
$itemid = $row[0];
$dispname = $row[1]; ?>
<tr align="center">
<?php print("<td align='left' class='table_col1'>$dispname</td>"); ?>
<td class="table_col2" align="center">
<?php echo $itemid; ?>
<input type="submit" class="button" name="remove" value="Delete
(<?php echo $itemid;?>)"
</td></tr>
<?php endwhile; ?>
</table></form>
POST form code..
if (isset($_POST["remove"])) {
while ($row = mysql_fetch_row($res)) {
$itemid = $row[0];
}
begin_frame();
echo $itemid;
end_frame();
}
If i move the brace after $itemid = $row[0] to below the last brace in the above section of the code it then passes the itemid of the first listed item of the table instead.
if (isset($_POST["remove"])) {
while ($row = mysql_fetch_row($res)) {
$itemid = $row[0];
begin_frame();
echo $itemid;
end_frame();
}
}
remove.php page code..
<?php
require_once("include/functions.php");
dbconn();
loggedinorreturn();
stdhead();
begin_frame();
$userid = ( int ) $USER['id'];
$qry = ("SELECT
carts.itemid,
items.name
FROM carts
INNER JOIN users ON carts.userid = users.id
INNER JOIN items ON carts.itemid = items.id
WHERE users.status = 'yes'
AND carts.userid = '$userid'
ORDER BY name DESC") or sqlerr(__FILE__, __LINE__);
$res = mysql_query($qry);
if (isset($_POST["remove"])) {
while ($row = mysql_fetch_row($res)) {
$itemid = $row[0];
}
begin_frame();
echo $itemid;
end_frame();
}
if (mysql_num_rows($res) > 0):
?>
<style type="text/css">
<!--
.button {
width: 220px;
height: 20px;
text-align: center;
background-image: url(images/btn.jpg);
background-color: #000000;
border-color: 3E3C32;
border-width: 1;
color: FFFFFF;
font-size: 8pt;
cursor: pointer;
}
-->
</style>
<form method="post" action="remove.php">
<table border="0" width="100%" class="main" cellpadding="4" cellspacing="0" align="center">
<tr>
<td class="colhead" width="1%" align="left">Item</td>
<td class="colhead" width="1%">
<center>Delete</center></td>
</tr>
<?php
while ($row = mysql_fetch_row($res)):
$itemid = $row[0];
$dispname = $row[1];
?>
<tr align="center">
<?php
print("<td align='left' class='table_col1'>$dispname</td>"); ?>
<td class="table_col2" align="center">
<?php echo $itemid; ?>
<input type="submit" class="button" name="remove" value="Delete
(<?php echo $itemid;?>)"
</td>
</tr>
<?php endwhile; ?>
</table>
</form>
<?php
echo "<br />";
endif;
end_frame();
stdfoot();
die;
{
stdhead("Nothing Found");
begin_frame("Nothing Found");
echo '<div style="margin-top:10px; margin-bottom:10px" align="center"><font size="2">Sorry, nothing found!</font></div>';
end_frame();
stdfoot();
}
?>
What would be advised to have the button pass in POST the corresponding $itemid of each row correctly I dont think POST is getting the $itemid and instead is using the last $itemid in the table via the while part of the code which is odd as it works to display each item in the table.
if (isset($_POST["remove"])) {
while ($row = mysql_fetch_row($res)) {
$itemid = $row[0];