表单提交后 PHP 会话变量出现问题 [英] Troubles with a PHP session variable after form submission

查看:24
本文介绍了表单提交后 PHP 会话变量出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我提交的 HTML 表单设置一个带有成功消息的 PHP 会话变量.如果没有表单错误,我会重定向到同一页面(使用 header())并显示来自会话变量的消息.到这里一切都很好,但是如果我在提交后再次访问该页面,消息仍然存在.是否可以仅在成功提交后重定向时显示?

I'm setting a PHP session variable with a success message for a HTML form that I'm submitting. If there are no form errors, I redirect to the same page (using header()) and display the message from the session variable. Everything is fine until here, but if I access that page again after submission, the message is still there. Is it possible to make it appear only when I redirect after a successful submit?

我的 form.php 代码如下所示:

My code for form.php looks like this:

if (isset($_POST['submit'])) {
 // some form processing here
 if (count($errors) == 0) {
   // some data saving here
   $_SESSION['status'] = 'Thank you for submitting the form';
   header('Location: /form.php');
}

现在是我的模板文件:

{if isset($smarty.session.status)} 
  <p><strong>{$smarty.session.status)</strong></p> 
{/if}

<!-- form html code goes here -->

谢谢.

推荐答案

销毁会话:

将您的代码更改为如下所示

Change your code to look something like this

if(isset($_SESSION['sentData'])
{
echo "Your message";

//This
session_destroy();
//or this
unset($_SESSION['sentData']);
}

其实不是,把这个放在form.php/你的模板文件/在你显示消息后:

Actually no, put this at the end of form.php/your template file/after you have displayed the message:

unset($_SESSION['status']);

这篇关于表单提交后 PHP 会话变量出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆