PHP隐藏表单作为get请求处理

时间:2014-01-06 15:25:29

标签: php html post get hidden-field

在PHP页面上,我的页面上有隐藏字段的按钮:

<form action="admin.php">
        <input name="action" value="delete" type="hidden">
        <input name="id" value="13" type="hidden">
        <input value="Delete user" type="submit">
</form>

在页面admin.php上我有这个:

switch ($_POST['action'])
{
    case 'delete' :
        if ($_SESSION['role'] == 2)
        {
            if ($user->deleteUser($_POST['id']))
            {
                $_SESSION['alert'] = "User deleted.";
                $_SESSION['alert_type'] = "success";
            } else
            {
                $_SESSION['alert'] = "Something went wrong while deleting the user.";
                $_SESSION['alert_type'] = "error";
            }
        } else
        {
            $_SESSION['alert'] = "You are not allowed to delete a user. You need to be an admin.";
            $_SESSION['alert_type'] = "error";
        }
        $data = $user->getProfiles();
        require 'Views/Admin.view.php';

 // etc

正如所料,admin.php加载了一个用户列表,每个用户旁边都有一个删除按钮。当我按下删除按钮时,用户被删除,但浏览器会在地址栏中显示隐藏字段的参数,就像它是GET请求一样。

发生了什么事?

1 个答案:

答案 0 :(得分:12)

GET是默认的表单操作。如果您需要POST必须指定它:

<form action="admin.php" method="post">