Php 使用onclick从while循环中发布单行值

Php 使用onclick从while循环中发布单行值,php,mysqli,Php,Mysqli,我试图从while循环生成的单行中传递列值。单击链接时,我希望该行中的某些值作为隐藏字段传递到另一个页面 守则: <form id="repeat" name="repeat" action="edit_user.php" method="post"> <?php $counter = 0; while($row = $result->fetch_array()) { $color = ($counter & 1)? "#D7ECEC" : "#DEDEDE";

我试图从while循环生成的单行中传递列值。单击链接时,我希望该行中的某些值作为隐藏字段传递到另一个页面

守则:

<form id="repeat" name="repeat" action="edit_user.php" method="post">
<?php
$counter = 0;
while($row = $result->fetch_array()) {
$color = ($counter & 1)? "#D7ECEC" : "#DEDEDE";
$counter++;
?>
<tr align="left" valign="middle" style="background: <?php print $color; ?>">
<td><a href="#" onclick="document.getElementById('repeat').submit();" >
    <?php echo $row['firstname']; ?> <?php echo $row['surname']; ?></a></td>
<td><?php echo $row['userlogin']; ?></td>
<td><?php echo $row['accesslvl']; ?></td>
<td ><?php echo $row['chgpasswrd']; ?></td>
</tr>
<input type="hidden" name="id" value="<?php echo $row['userID']; ?>" />
<input type="hidden" name="lvl" value="<?php echo $row['accesslvl']; ?>" />
<?php } ?>
</table>
</form>


你真的需要把它分成两页。第一个页面显示每个唯一用户的用户信息和链接,第二个页面提供用于发布表单数据的编辑表单,如下所示

<table>
<?php
$counter = 0;
while($row = $result->fetch_array()) {
    $color = ($counter & 1)? "#D7ECEC" : "#DEDEDE";
    $counter++;
?>
<tr align="left" valign="middle" style="background: <?php print $color; ?>">
    <td>
        <a href="edit_user.php?userID=<?php echo $row['userID']; ?>&accesslvl=<?php echo $row['accesslvl']; ?>">
            <?php echo $row['firstname']; ?> <?php echo $row['surname']; ?>
        </a>
    </td>
    <td><?php echo $row['userlogin']; ?></td>
    <td><?php echo $row['accesslvl']; ?></td>
    <td ><?php echo $row['chgpasswrd']; ?></td>
</tr>
<?php } ?>
</table>

在“编辑用户”页面上,可以使用url查询字符串传入的信息填充隐藏字段。记住清除这些值以防止sql注入或xss攻击。我在这里使用了htmlentities作为示例,您可能希望增强这一点,请阅读htmlentities上的php文档页面以获取更多信息(entu引号)

$userID=htmlentities($\u GET['userID']);
$accesslvl=htmlentities($_GET['accesslvl']);

在这里使用JS有什么具体原因吗?如果客户端关闭了JSNo,它就会失败。如果不需要,我宁愿不使用JS。具体来说,我不想发送URL中的值。谢谢,您可以将隐藏值构建到链接的url中,这可能会帮助name=“id[]”和name=“lvl[]”。感谢您的回复。我已经做了更改,它仍然在发送循环中最后一行的值。还有其他建议吗?干杯可能会将它们添加到其中一个标签中,而且您没有打开的表格标签感谢您的回复。我已经尝试过了,但是仍然得到了相同的结果。谢谢,但是您只提交了一个表单,因此每次运行while循环时,它都会替换id和lvl的值,因此一旦循环完成,您将只得到最后两个值。更好的方法是将这些值添加到链接本身,使链接成为edit_user.php?id=
$userID    = htmlentities($_GET['userID']);
$accesslvl = htmlentities($_GET['accesslvl']);

<form action="modify_user.php" method="post">
    <input type="hidden" name="id" value="<?php echo (int)$userID; ?>" />
    <input type="hidden" name="lvl" value="<?php echo $accesslvl; ?>" />

    //rest of form html