tags:

views:

200

answers:

3

Hello,

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.

A: 

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.

slifty
I don't think I can do that because I'm setting the message and then I redirect.
Psyche
I don't want to use any $_GET variables.
Psyche
Well, as you can see from the code above I can't do any operations after displaying the message.
Psyche
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.
slifty
The only way I can unset it is in the template file. I don't see any other way.
Psyche
Is not ok to pass the message within a template variable because that variable will be set only after form submission, and it won't have a value after redirect.
Psyche
I was saying that you keep using a session to pass the variable across the redirect, but you don't reference the session in the template. Instead, you load the session into a variable BEFORE the template (but after the redirect) (at which point you can wipe the session), pass the variable to the template as a template parameter, and reference the template parameter instead.
slifty
Can you please provide an example? Thanks.
Psyche
+1  A: 

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']);
Chief17
A: 

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 :)

Jacob