【问题标题】:I need help to solve a nested FORM problem我需要帮助来解决嵌套的 FORM 问题
【发布时间】:2021-04-06 11:58:43
【问题描述】:

有一个从 MySQL 数据生成的表,每一行都有一个包含多个表单的字段。

我需要在每行的第一个字段上放置一个复选框,包含唯一 ID,然后我需要通过 POST 使用位于表外的按钮传递多个复选框值。

当嵌套表单无法完成时,我如何获得它?

这是我的代码:

    <!-- This button needs to be outside of the table -->
    <button type="submit" form="selected_checkboxes">Send checked boxes</button>

    <table>
    <thead>
    <tr>
    <th>Select</th>
    <th>Name</th>
    <th>Action</th>
    </tr>
    </thead>
    <tbody>

    <!-- If I put the form there, for the selected checkbox, it won't work, because the other forms
    are nested inside the table -->
    <form action="page.php" method="POST" name="selected_checkboxes">  <!-- THIS WON'T WORK -->

    <?php
    $db = $conn->prepare('SELECT * FROM accounts WHERE name = ? ORDER BY id DESC');
    $db->bind_param('i', $name);
    if (!$db->execute()) die('Error while fetching accounts: '. $conn->error);
    $res = $db->get_result();
    while ($data = $res->fetch_assoc()) {
        $id = $data['id'];
        $name = $data['name'];
        echo '<tr>
        <td><input type="checkbox" name="list[]" value="'.$id.'"></td>
        <td>'.$name.'</td>
        <td>
            <form action="page.php" method="POST"><input type="hidden" name="id" value="'.$id.'" /><button type="submit" name="action1">Send</button></form>
            <form action="page.php" method="POST"><input type="hidden" name="id" value="'.$id.'" /><button type="submit" name="action2">Archive</button></form>
        </td>
        </tr>
    }
    ?>
    
    </form>  <!-- TO CLOSE THE NOT WORKING FORM -->
    </tbody>
    </table>

你会如何解决这个问题?

【问题讨论】:

  • 在代码中使用die(mysqli_error($$conn)); 是一个非常糟糕的主意,因为它可能会泄露敏感信息。更多解释见这篇文章:mysqli or die, does it have to die?
  • @Dharman 我很感激,但这是一个个人项目,所以我不怎么看,但是谢谢
  • 因为他们都去同一个地方,只需使用外部形式并测试各个动作($action1,$action2)
  • 为什么?为什么您会忽略我的建议以使您的代码更简单更好?如果您只学习 PHP,那么请从学习最简单的解决方案开始。不要过于复杂。
  • @Dharman 不是让它更简单或更好,因为它是个人项目,甚至没有人会看到它

标签: php html forms post submit


【解决方案1】:
<td>
    <form action="page.php" method="POST"><input type="hidden" name="id" value="'.$id.'" />
    <button type="submit" name="action1">Send</button>
    </form>
    <form action="page.php" method="POST"><input type="hidden" name="id" value="'.$id.'" />
    <button type="submit" name="action2">Archive</button>
    </form>
    </td>

在本节中,我将使用热链接而不是表单对其进行修改,并制作不同的处理页面,例如:

echo "<td><a href='page2.php?action=send&id=".$id."'>Send</a> ";
echo "<a href='page2.php?action=archive&id=".$id."'>Send</a></td>";

page2.php 将在 $_GET["action"] 和 $_GET["id"] 上运行,就像替换的迷你表单一样。

那么你从一开始的表格(带有复选框,你说它不起作用)将是唯一的一个并且可以工作:) 仅当您授予经过验证的用户访问权限时,这才可以。

希望这会推动你前进。

:)

【讨论】:

  • 这实际上对我帮助很大,我没有想过将嵌套表单转换为 GET 链接。谢谢!
  • 很高兴我把它向前推了一点:)
【解决方案2】:

假设您实际上是在操作数据而不是简单地请求视图,则 POST 是适合使用的动词。

您可以简单地使用所选项目的 id 索引提交按钮。我已将您的脚本放入处理潜在用户输入的标准格式(假设此脚本是 page.php),然后显示表单:

<?php
// get $conn and other config

if(!empty($_POST['selected_checkboxes']) {
    // iterate through $_POST['id_list']
    header(location: '/page/to/show/on/completion');
    exit;
}

if(!empty($_POST['send']) {
    $arr = (array)$_POST['send'];
    $id = array_keys($arr)[0];
    // do what you need to do with the id
    header(location: '/page/to/show/on/completion');
    exit;
}

if(!empty($_POST['archive']) {
    $arr = (array)$_POST['archive'];
    $id = array_keys($arr)[0];
    // do what you need to do with the id
    header(location: '/page/to/show/on/completion');
    exit;
}
$db = $conn->prepare('SELECT * FROM accounts WHERE name = ? ORDER BY id DESC');
$db->bind_param('i', $name);
$accountList = $db->get_result();
?>
<html>
...
<form action="page.php" method="POST">
    <button type="submit" name="selected_checkboxes">Send checked boxes</button>
    <table>
        <thead>
            <tr>
                <th>Select</th>
                <th>Name</th>
                <th>Action</th>
            </tr>
        </thead>
        <tbody>
        <?php while($data = $accountList->fetch_assoc()): ?>
            <tr>
                <td><input type="checkbox" name="id_list[]" value="<?= $data['id'] ?>"></td>
                <td><?= $data['name'] ?></td>
                <td>
                    <button type="submit" name="send[<?= $data['id'] ?>]">Send</button>
                    <button type="submit" name="archive[<?= $data['id'] ?>]">Archive</button>
                </td>
            </tr>
        <?php endwhile; ?>
        </tbody>
    </table>
</form>  

【讨论】: