PHP : echo message for a specified amount of time

Jeffrey picture Jeffrey · May 13, 2011 · Viewed 7.7k times · Source

I want to display a string "Your status has been posted" for something around 3 or so seconds than I want it to go away.

As of right now I have a news feed where the user can post messages and after they post it echoes that string of text until the url is re-enetered. Does anyone have any suggestions?

if ($_POST['submit'])
{
$body = $_POST['body'];

if ($body)
{
    include ('connect.php');
    $date = date("Y-m-d");
    $email = $_SESSION['email'];
    $who = mysql_query("SELECT firstname FROM people WHERE email = $email");
    $insert = mysql_query("
    INSERT INTO status VALUES('','$email','$body','$date')");
    echo ('Your status has been posted <p></p>');
}
else
    echo 'Status required to post into news feed! <p></p>';
 }
 ?>

Status :

Thanks for your help!

Jeff

Answer

Alin Purcaru picture Alin Purcaru · May 13, 2011

You need to add some JavaScript to do this. Here is an outline to get you started:

<p id="info-message">Your status has been posted</p>
<script>
  setTimeout(function(){
    document.getElementById('info-message').style.display = 'none';
    /* or
    var item = document.getElementById('info-message')
    item.parentNode.removeChild(item); 
    */
  }, 3000);
</script>

Also you may want fade-out effects. For that I'd recommend using a library like jQuery or Scriptaculous.

Useful links:
https://developer.mozilla.org/en/Window.setTimeout
http://api.jquery.com/fadeOut/
http://script.aculo.us/


Oh, by the way, your script is vulnerable to SQL injection. Always escape your query parameters (or use prepared queries)!