如何获取表单提交状态
我正在寻找一种“整洁”(干净)的方式来在提交表单后显示状态消息。
例如,如果用户更新他们的个人资料,我想显示一个带有文本“您的个人资料已更新!”的 DIV。
现在我正在这样完成此操作:
在发布数据并插入或更新数据库表后,我执行 header("Location: updateprofile.php?status=updated");
这工作得很好,但它不是很干净。我不需要查询字符串。
我尝试的另一种方法是设置变量 $subscribed=1;并且之后不进行重定向。但是,当用户更新其信息时,它仍然在表单字段中显示旧信息,直到刷新。所以这也行不通。
我知道我可以使用 AJAX 提交表单并以这种方式完成它,但是,对于我正在做的事情,我不想使用 AJAX。我想要一个服务器端解决方案。
我唯一的另一个想法是必须设置 $_SESSION 变量,然后在显示状态更新后以某种方式将其删除。
任何想法将不胜感激!
谢谢,
托尼
I am looking for a "neat" (clean) way to show a status message after submitting a form.
For example, if a user updates their profile, I want to show a DIV with the text "Your Profile Has Been Updated!".
Right now I am getting this done this way:
After posting the data and either inserting or updating the database table, I do a header("Location: updateprofile.php?status=updated");
This works just fine and dandy, however it isn't very clean. I don't want the query string.
Another method I tried was to set a variable of $submitted=1; and not do a redirect after. However, when a user is updating their information it still shows the old information in the form fields until a refresh. So this doesn't work either.
I know I could use AJAX to submit the form and accomplish it that way, however, for what I am doing I do not want to use AJAX. I want a server side solution.
My only other thought is having to set a $_SESSION variable then somehow remove it after showing the status update.
Any ideas would be appreciated!
Thanks,
Tony
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
大多数时候,这是通过存储需要显示的任何闪存消息的会话变量来处理的。
在共享视图(布局、元素)中,将
$_SESSION['flashMessage']
设置为“您的个人资料已更新”。在处理表单提交的任何代码中。Most of the time this is handled through session variables that store whatever flash messages need to be shown.
In a shared view (layout, element) put something like
Set
$_SESSION['flashMessage']
to "Your profile has been updated." in whatever code is handling your form submissions.将状态存储在会话数组变量中,然后只需执行以下操作:
Store the statuses in a session array variable, then simply do: