开发者

PHP variable update using jquery

开发者 https://www.devze.com 2023-02-26 18:09 出处:网络
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

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 ec开发者_JAVA技巧ho $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> 


This line

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

Should be

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


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

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


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


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)


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;
            }
     });


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 ( + )

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号