我需要帮助来解决嵌套的 FORM 问题

I need help to solve a nested FORM problem

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

我需要在每行的第一个字段上放置一个包含唯一 ID 的复选框,稍后我需要通过 POST 使用位于 [=23= 之外的按钮传递多个复选框值].

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

这是我的代码:

    <!-- 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>

你会如何解决这个问题?

<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"] 上运行,就像替换的迷你表单一样。

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

希望这会推动你前进。

:)

假设您实际上是在操作数据,而不是简单地请求查看,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>