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

时间:2020-12-29 21:47:40

标签: php html forms post submit

有一个从 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>

你会如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

<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"]。

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

希望这会推动你前进。

:)

答案 1 :(得分:0)

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