Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display message only for the first page load

Tags:

php

I would like to display thank you message after adding comment only for the first page load... The comment form is processed using an external php file and than redirected back to the page. I would like to display some message after the redirection only... What would be the best way to do this using php?

like image 906
King Julien Avatar asked Aug 16 '10 14:08

King Julien


3 Answers

Assuming you have access to the external php file that processes the file you could do something similar to the following on the processing file:

$_SESSION['flashMessage'] = 'Thank you for posting.';
header("Location: your-page.php');

And then add the following to the redirect page:

if ($_SESSION['flashMessage']) {
    echo $_SESSION['flashMessage'];
    $_SESSION['flashMessage'] = NULL;
}
like image 174
simnom Avatar answered Sep 24 '22 05:09

simnom


Save the mesage into a session. Display it, and after just unset the session variable.

like image 43
Gatman Avatar answered Sep 24 '22 05:09

Gatman


On the page where the comment is processed:

if($success)
{
    $_SESSION['userMsg'] = "<p>Your comment has been added. Thank you.</p>";
}

In any/all pages (but mainly the one you're redirecting to):

if($_SESSION['userMsg'] != '')
{
    print $_SESSION['userMsg'];
    unset($_SESSION['userMsg'];
}

This is assuming you're using Sessions and have therefore previously called the session_start() function

like image 37
Brendan Bullen Avatar answered Sep 24 '22 05:09

Brendan Bullen