具有多个tbody和多个具有相同名称的隐藏字段的表单仅发送表单中的最后一个隐藏字段

时间:2013-03-10 21:22:44

标签: php html

此表单在一种形式中有多个tbody。 每个tbody包含具有“唯一”值的“隐藏”字段。 但是,当发送此表单时,它只发送最后一个“pouzivatel_id”值。

e.g。

我想执行“pouzivatel_id”= 4,但表单发送“pouzivatel_id”=“1”

如何在不使用多种表单的情况下解决此问题?

<form method="post" action="lib/action/admin.post.php">
<table>
    <thead>
        <tr>
            <th>Osobné číslo</th>
            <th>Prezývka</th>
            <th>Meno</th>
            <th>Priezvisko</th>
            <th>Operácie</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>00000000004</td>
            <td>user4</td>
            <td>user</td>
            <td>user</td>
            <td>
                <input type="hidden" name="pouzivatel_id" value="4"/>
                <button type="submit" name="ziadatel-schvalit" class="tlacidlo zelena">Schváliť</button>
                <button type="submit" name="ziadatel-zmazat" class="tlacidlo cervena">Zamietnuť</button>
            </td>
        </tr>
    </tbody>
    <tbody>
        <tr>
            <td>00000000003</td>
            <td>user3</td>
            <td>user</td>
            <td>user</td>
            <td>
                <input type="hidden" name="pouzivatel_id" value="3"/>
                <button type="submit" name="ziadatel-schvalit" class="tlacidlo zelena">Schváliť</button>
                <button type="submit" name="ziadatel-zmazat" class="tlacidlo cervena">Zamietnuť</button>
            </td>
        </tr>
    </tbody>
    <tbody>
        <tr>
            <td>00000000002</td>
            <td>user2</td>
            <td>user</td>
            <td>user</td>
            <td>
                <input type="hidden" name="pouzivatel_id" value="2"/>
                <button type="submit" name="ziadatel-schvalit" class="tlacidlo zelena">Schváliť</button>
                <button type="submit" name="ziadatel-zmazat" class="tlacidlo cervena">Zamietnuť</button>
            </td>
        </tr>
    </tbody>
    <tbody>
        <tr>
            <td>00000000001</td>
            <td>user1</td>
            <td>user</td>
            <td>user</td>
            <td>
                <input type="hidden" name="pouzivatel_id" value="1"/>
                <button type="submit" name="ziadatel-schvalit" class="tlacidlo zelena">Schváliť</button>
                <button type="submit" name="ziadatel-zmazat" class="tlacidlo cervena">Zamietnuť</button>
            </td>
        </tr>
    </tbody>
</table>

1 个答案:

答案 0 :(得分:1)

您可以为每个提交按钮使用不同的名称。如果按下提交按钮,则只有此提交按钮会发送到您的PHP脚本。在PHP脚本中,检查按下了哪个按钮。

示例HTML代码:

<input type="hidden" name="pouzivatel_id[]" value="2"/>
<button type="submit" name="ziadatel-schvalit_2" class="tlacidlo zelena">Schváliť</button>
<button type="submit" name="ziadatel-zmazat_2" class="tlacidlo cervena">Zamietnuť</button>
<input type="hidden" name="pouzivatel_id[]" value="1"/>
<button type="submit" name="ziadatel-schvalit_1" class="tlacidlo zelena">Schváliť</button>
<button type="submit" name="ziadatel-zmazat_1" class="tlacidlo cervena">Zamietnuť</button>

示例PHP代码:

<?PHP
foreach($_POST['pouzivatel_id'] as $id)
{
    if(isset($_POST['ziadatel-schvalit_' . $id]))
    {
        //Schváliť
    }
    elseif(isset($_POST['ziadatel-zmazat_' . $id]))
    {
        //Zamietnuť
    }
}
?>