2

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?

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');
}

And now my template file:

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

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

Thank you.

3 Answers 3

2

Destroy the session:

Change your code to look something like this

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

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

EDIT:

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

unset($_SESSION['status']);
Sign up to request clarification or add additional context in comments.

Comments

0

Before displaying the message you could do a check that the message is not empty. If it isn't empty then display the message and immediately afterwards explicitly empty the session variable so that next time the preceding check won't display the message.

I hope I understood your question :)

Comments

0

Would it be enough to just clear the session variable after you display the message?

That seems like the most straightforward solution. You could also:

A) Check to see what information lies in the $_SERVER variable to try to detect how the user got to the page, only displaying the message if it happened from the rout.

B) Have the redirect include a querystring variable (for instance '?messages=1') which you check using $_GET before displaying the session message.


EDIT: I thought the order of operations is:

1) form submit 2) if no errors, set message in session 3) redirect 4) display the message after the redirect

I'm saying add step 5: delete the message AFTER it is displayed. As in, echo the message like you are doing, but then add code after the echo to delete or clear the session variable.


EDIT 2: Ahh, I understand now...

I'm curious if you should be referencing the session variable directly from your template file. Might it be better to pass it in as a template parameter that gets set by the PHP which calls the template? This gives you the ability to separate session management from display, which is probably in your best interest anyway.

8 Comments

I don't think I can do that because I'm setting the message and then I redirect.
I don't want to use any $_GET variables.
Well, as you can see from the code above I can't do any operations after displaying the message.
I hear ya -- I wouldn't do the $_GET solution either but I thought I would mention it just in case. What about the clarification I made after the edit line? If I understand your situation properly, that should do the trick... You set the message, you redirect, you display it, then you unset the message.
The only way I can unset it is in the template file. I don't see any other way.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.