0

I am using the following code but I can't seem to get it to work. If anyone could help or point me in the right direction that would be great. What I'm trying to achieve is a facebook style of alert where the alert will appear without refreshing the page. The number of alerts come from the number of rows in the database. Am I doing anything stupid?

jQuery:

<script type="text/javascript">
var alertNumber = <?php echo $display['alertNumber']; ?>;
setTimeout(checkVariableValue, 2000);
function checkVariableValue() {
     $.ajax({
        url: 'jquery.php?query='.alertNumber,
        success: function(newAlert) {
            if (newAlert != alertNumber);
                alertNumber = newAlert;
                document.getElementById("wibble").innerHTML=alertNumber;
            }
     });
}
</script> 
4
  • Read up 'Long Polling' stackoverflow.com/questions/333664/… Commented Apr 19, 2011 at 14:38
  • well what's it doing? something? nothing? Commented Apr 19, 2011 at 14:39
  • @Groovetrain it does nothing at all apart from duplicate the page within itself. Commented Apr 19, 2011 at 14:41
  • thank you for all your answers they were all helpful, but the main issue was the +alertNumber Commented Apr 19, 2011 at 14:52

6 Answers 6

6

This line

url: 'jquery.php?query='.alertNumber,

Should be

url: 'jquery.php?query='+alertNumber,
Sign up to request clarification or add additional context in comments.

2 Comments

locrizak do you know how I would go about looping that function so that it keeps on checking say every 5 seconds?
@David instead of setTimeout you need setInterval. setTimeout will only execute once where setInterval continues until you clear it.
3
url: 'jquery.php?query='.alertNumber,

is in my eyes wrong. it must be url: 'jquery.php?query=' + alertNumber,

Comments

2

Maybe I'm missing something, but I do not see your page actually echoing any text for your function to read.

Comments

1

My apologies for an answer. Others seemed to have found the issue (or at least one), I just felt this was too dense for a comment.

I would recommend altering your style to have a "read flag" in the database instead of holding on to the last row (or in your case row count) and using that as a discriminator. As the messages grow, you wouldn't want (presumably) hundreds of results dumping through the AJAX call. Also, unless you're presuming that once it's been displayed on the page the user has acknowledged it. (An example would be a last-minute message pops up and the user clicks a link at the same time--now there's no visibility).

Also, if you find the site to be gaining a lot of traffic, it may be worth while looking in to a PUSH service (an article I found quickly googling goes on about it and its benefits)

Comments

0

Right now your php code doesn't seem to be sending back any response.

Once you get the number of rows in the variable $q, just add this line

echo $q;

You may also need to add dataType option in the ajax request to specify that you are expecting back a response of type text

$.ajax({
        url: 'jquery.php?query='+alertNumber,
        dataType : 'text',
        success: function(newAlert) {
            if (newAlert != alertNumber);
                alertNumber = newAlert;
                document.getElementById("wibble").innerHTML=alertNumber;
            }
     });

2 Comments

text is not a valid dataType. Options are xml, json, script, or html so it it should be html
Oh, my mistake. I guess text is never used in the intelligent guessing, and html is used instead. Thanks.
0
var alertNumber = <?php echo $display['alertNumber']; ?>;
setTimeout(checkVariableValue, 2000);
function checkVariableValue() {
     $.ajax({
        url: 'jquery.php?query='.alertNumber,

see that line on url property? that line isn't inside php script... so you must use a javascript concatenation operator ( + )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.