dounianxie2058 2018-11-20 12:45
浏览 21

单击按钮时POST不传递所选数据

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];
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
    • ¥15 关于#Java#的问题,如何解决?
    • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
    • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
    • ¥15 cmd cl 0x000007b
    • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
    • ¥500 火焰左右视图、视差(基于双目相机)