0

我有一个应用程序,我在其中使用 URL 参数在某个操作和重定向后触发结果消息。示例流程如下:

1. User fills out form and clicks "Update"
2. [PHP code to update DB]
3. header('Location: [next-page.php]?message=1')
4. [PHP code on checks DB for the text of message id #1 and echo the message, (e.g. "Update successful!")]

这最初运作良好,但有几个问题。URL 参数在消息显示后仍然存在,因此如果页面被刷新,或者如果用户稍后单击返回该页面,则消息会再次显示。我正在寻找一种方法来执行上述操作,但仅在第一次显示该消息。我想避免为每条结果消息创建一个数据库实体,并带有一个属性来判断它是否已经被查看过,因为这似乎有点矫枉过正。你会怎么处理这个?

4

1 回答 1

0

一种方法是将表单发布到 next-page.php,然后在页面上执行此代码:

if (isset($_POST['form_element']))
    include("form-process.php"); //Or whatever page the DB update goes in

编辑:您也可以使用 Javascript 来完成。将其放在表单所在的页面上:

$("#ajax_submit").click(function(){
    $("#ajax_form").submit();
    window.location=location.protocol+'//'+location.host+location.pathname+"nextpage.php";
});

在表单如下的页面上:

<form action="process_form.php" method="POST" id="ajax_form">
    Name: <input name="username"><br>
    Password: <input name="password" type="password"><br>
    <input type="submit" value="Login" id="ajax_submit">
</form>
于 2012-05-16T18:59:39.680 回答