如何区分表单中的按钮点击?

时间:2014-09-23 01:59:11

标签: php html forms

所以我正在为我的网站创建一个数据库,我想创建一个允许您在表中添加或删除的管理部分。这是我想要实现的目标的快照......

enter image description here

在我的php文件中,我if($_POST['delete_category'])正确获取了删除按钮的点击次数,但后来我不确定如何区分实际点击的删除按钮。我确定这是一个非常简单的解决方案,但我被困住了。谢谢!

1 个答案:

答案 0 :(得分:2)

您可以识别以下标记提交的按钮(基于您从DB获取的示例结果):

<?php

if(isset($_POST['delete_category'])) {
    $id = $_POST['delete_category']; // the value="1" or value="3" goes in here
    echo $id;
}


?>

<form method="POST" action="">
    <table border="1">
        <tr>
            <th>ID</th><th>Name</th><th>Delete</th>
        </tr>
        <tr>
            <td>1</td>
            <td>Senior Pictures</td>
            <td><button typpe="submit" name="delete_category" value="1">Delete</button></td>
            <!-- each delete button has the same name, but different values -->
        </tr>
        <tr>
            <td>3</td>
            <td>Engagements</td>
            <td><button typpe="submit" name="delete_category" value="3">Delete</button></td>
        </tr>
    </table>
</form>

如果我不得不猜测这对取物有意义:(注意:只是样品!

<form method="POST" action="">
    <table border="1">
        <tr>
            <th>ID</th><th>Name</th><th>Delete</th>
        </tr>
        <?php while($row = $results->fetch_assoc()): ?> <!-- assuming mysqli() -->
        <?php while($row = mysql_fetch_assoc($result)): ?> <!-- assuming on mysql (bleh) -->
            <tr>
                <td><?php echo $row['id']; ?></td>
                <td><?php echo $row['name']; ?></td>
                <td>
                    <button typpe="submit" name="delete_category" value="<?php echo $row['id']; ?>">Delete</button>
                </td>
            </tr>
        <?php endwhile; ?>
        </table>
</form>
相关问题