views:

306

answers:

3

Hello. I am storing a session variable when a comment is inserted.. called user_message.. and i want to, when user_message, have been stored, THEN, show message on index.php index.php is right now:

<div id="message">
<? echo $_SESSION["user_message"]; ?>
</div>

But you will need to refresh(f5) the site, to see the message AFTER its stored..

But i want to if variable user_message has got stored something, then displays it..

Hope you understand.

A: 
<?php if( isset($_SESSION["user_message"]) ): ?>
  <div id="message">
    <? echo $_SESSION["user_message"]; ?>
  </div>
<?php endif; ?>
Seb
A: 

I'm not sure I understand your intention - do you want to refresh the page the user is on, once he/she has sent a new comment ?

If so - if you're using a normal form post/get, then you can redirect the user request to which ever page you want, and show a notice, using the code @Seb wrote above.

If you're using ajax to send the comment, you can return a response from the ajax call, notifying the javascript function that created the ajax call that the page needs to refresh, and then set the location.href variable to what ever url you want.

However - if you want to refresh the page for all users seeing the comments page, once another user has sent a comment, you will need to have a repeated ajax call every few seconds/minutes, that checks if something has changed in the server, and if so use the location.href variable to redirect the user to a different. You can't do this if you're relying only on the session variable saved on the computer of the commenter, as it is only accessible when the original commenter is using your site.

Doron
A: 

I recommend some Javascript if you want the message to be displayed on the same page as the comments form. If you're using jQuery http://jquery.com/ then you can do something like.

$('form#CommentFormID').submit(function() {
    var message = $('form#CommentFormID textarea#comment').html();
    $('body').append('<div id="message">' + message + '</div');
});

Haven't tried this, but it should work fine.

Giles Van Gruisen