views:

375

answers:

4

How do i do in jquery, to check if there's anything in isset SESSION user_message, each 1 seconds, and if there is then display #box else dont.. thanks

A: 

Use PHP to echo the jQuery command:

if (isset($_SESSION['user_message'])) {
    echo '<script> $("#box").… </script>';
}

This will only echo the following if $_SESSION['user_message'] is set:

<script> $("#box").… </script>
Gumbo
This does not check if there's anything in isset every 1 second.. It only check once, and if there is it shows, but i want to check if there's anything in isset session user_message, each 1 second, for new stored user_message.. get me?
Karem
@Azzyh: That would require to send a request to the server every second (for every user)! That are really a lot of requests. Isn’t it sufficient to do that with every request the user initializes when clicking a link etc. like SO does?
Gumbo
how do i do this anyway? i dont think it will hurt so much, i mean i do not know how to do it otherwise..When saving a comment, its storing a session variable user_message, and i dont know how then to do if its stored then display message.. So i just wanna know how to do this anyway?
Karem
@Azzyh: I hope you’re aware of that the value of `$_SESSION` is not shared between all users of a website. It’s rather only available in the current session. So a comment that’s stored there will only available for the one who wrote that comment.
Gumbo
@Gumbo i know that, i am not storing a comment there, but only a message to the one who commented "You have earned +2 points"..
Karem
@Azzyh: How do you do that? Where is the session data stored and how do you associate the session with a user? Anyway, take a look at jQuery’s Ajax methods (see http://api.jquery.com/category/ajax/).
Gumbo
I am storing using this variable after inserted onto database in insert.php: $_SESSION["user_message"] = "You were awarded +$earning earni points."; and what i want is, when its stored then it should index.php should display #box, and then set the session variable to NULL;.
Karem
A: 

Simple as this:

<?php
if (isset($_SESSION['user_message']))
{
?>

<script>
$('#box').show(2000);
</script>

<?php
}
?>

Or you can use fadeIn or fadeOut with specified time.

Sarfraz
This does not check if there's anything in isset every 1 second..
Karem
@Azzyh: could you elaborate more on your question about every one second, what do you want to do, do you know the overhead that will be caused every one second?
Sarfraz
A: 
<?php if (isset($_SESSION['user_message']))  { ?>
    <script type="text/javasscript">
        $("#box").show(1000);
    </script>
<?php } ?>
Tim S. Van Haren
Or alternative syntax: http://php.net/manual/en/control-structures.alternative-syntax.php way better to read imho.
Felix Kling
This does not check if there's anything in isset every 1 second..
Karem
+2  A: 

PHP file (session.php):

<?php if( isset($_SESSION) ){echo 1;}?>

JS file:

function checkSession(){
    $.ajax({url: "session.php", success: function(data){
         if( data == 1){
             $('#box').show();
         }else{
             $('#box').hide();
         }
    }});
}
setInterval('checkSession()',1000);

This is untested, but it should do what you're looking for. Note that, typically, AJAX request expect XML or JSON, so you can reformat the session.php file the way you want.

cmptrgeekken
I think you should use setInterval() rather than setTimeout()
Kenaniah
Ah, never used `setInterval` before.
cmptrgeekken
setInterval takes a function, not an eval string. setInterval(checkSession, 1000);
LiraNuna
If i want to set the SESSION variable to NULL; where should i do this? thank you
Karem
@LiraNuna: Looks like setInterval can take either a function or an eval string.@Azzyh: You'd have to do that in your PHP script by calling `session_destroy()` or something similar. It may work better if you check for a specific value in the `$_SESSION` variable, since it may always be set.
cmptrgeekken
cmptrgeekken: That's news for me, thanks for teaching me something new :)
LiraNuna
@Cmptrgeekken, its just because i dont really know exactly where i should destroy/set the variable to null, in the code.. If i do it when it echo 1; then after 1 second the message will disappear.. do you understand me? thank you for taking your time helping me
Karem
When do you need to set the variable to null? Do you want to show the div if the session is set, destroy the session, then keep the div displayed? Maybe you could show us what the div contains.
cmptrgeekken