可以重定向发布数据吗?

时间:2016-06-18 09:36:32

标签: php

我需要帮助'确认表单重新提交'消息(在观看视图源时获取chrome)

如果我删除评论,它只会显示用户名1,无论我选择哪个ID,如果评论保留为以下代码,则可以使用重新提交消息...

<?php

    $username = get_username_from_db(1);

    if (isset($_POST['id'])) {
        $id = $_POST['id'];
        $username = get_username_from_db($id);

        /*
        // if this comment stay as comment, username will update with 'confirm form resubmission' message
        // else 'confirm form resubmission' message not shows but username not updated (keep as default [1])

        header('Location: ' . $_SERVER['HTTP_HOST']);
        exit();
        */
    }

?>

<body>
    <form method="POST">
        <fieldset>
            <legend>Choose ID</legend>
            <select name="id" onchange="this.form.submit()">
                <option value="1">1st</option>
                <option value="2">2nd</option>
                <option value="3">3rd</option>
            </select>
            <br>
            <label>Username:</label>
            <input type="text" name="username" value="<?php echo $username; ?>"> 
        </fieldset>
    </form>
</body>

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

试试这个:

代码$ _GET;

<?php

    $username = get_username_from_db(1);

    if (isset($_POST['id'])) {
        $id = $_POST['id'];
        $username = get_username_from_db($id);

        /*
        // if this comment stay as comment, username will update with 'confirm form resubmission' message
        // else 'confirm form resubmission' message not shows but username not updated (keep as default [1])
        */
        $url = $_SERVER['HTTP_HOST'] . '?name='.$username;
        header('Location: ' . $url);
        exit();

    }
    else if(isset($_GET['name']) && !empty($_GET['name'])){
        $username = $_GET['name'];
    }
?>

<body>
    <form method="POST">
        <fieldset>
            <legend>Choose ID</legend>
            <select name="id" onchange="this.form.submit()">
                <option value="1">1st</option>
                <option value="2">2nd</option>
                <option value="3">3rd</option>
            </select>
            <br>
            <label>Username:</label>
            <input type="text" name="username" value="<?php echo $username; ?>"> 
        </fieldset>
    </form>
</body>

代码为$ _SESSION;

<?php

session_start();
$user_id = 1;

if(isset($_SESSION['user_id'])){
    $user_id = intval($_SESSION['user_id']);
}
// prevent 0 and negative;
if($user_id <= 0){
    $user_id = 1;
}
$username = get_username_from_db($user_id);

if(isset($_POST['id'])){
    $id = intval($_POST['id']);
    // prevent 0 and negative;
    if($id <= 0){
        $id = 1;
    }
    $_SESSION['user_id'] = $id;
    $url = $_SERVER['HTTP_HOST'];
    header('Location: ' . $url);
    exit();
}
?>

<body>
    <form method="POST">
        <fieldset>
            <legend>Choose ID</legend>
            <select name="id" onchange="this.form.submit()">
                <option value="1">1st</option>
                <option value="2">2nd</option>
                <option value="3">3rd</option>
            </select>
            <br>
            <label>Username:</label>
            <input type="text" name="username" value="<?php echo $username; ?>"> 
        </fieldset>
    </form>
</body>