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
2010-02-22 18:22:55
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
2010-02-22 18:33:46
@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
2010-02-22 18:41:14
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
2010-02-22 18:54:04
@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
2010-02-22 19:09:48
@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
2010-02-22 21:45:10
@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
2010-02-22 21:59:32
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
2010-02-22 22:33:47
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
2010-02-22 18:23:35
A:
<?php if (isset($_SESSION['user_message'])) { ?>
<script type="text/javasscript">
$("#box").show(1000);
</script>
<?php } ?>
Tim S. Van Haren
2010-02-22 18:24:02
Or alternative syntax: http://php.net/manual/en/control-structures.alternative-syntax.php way better to read imho.
Felix Kling
2010-02-22 18:25:37
+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
2010-02-22 19:26:20
setInterval takes a function, not an eval string. setInterval(checkSession, 1000);
LiraNuna
2010-02-22 21:30:03
If i want to set the SESSION variable to NULL; where should i do this? thank you
Karem
2010-02-22 22:38:31
@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
2010-02-22 22:57:37
cmptrgeekken: That's news for me, thanks for teaching me something new :)
LiraNuna
2010-02-22 23:59:07
@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
2010-02-23 09:54:12
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
2010-02-23 14:47:29