Jump to content

loop AJAX to check PHP script


emoubi

Recommended Posts

Hi folks,

 

I'm reading some posts and examples about when a user clicks on a button, focus an element and etc to execute some AJAX. My case is that I have a web page and the user do not interact with it. And for example I have a php script that do some selects to a database. In normal work the result of that php script will be 0 ($num_rows = 0). But sometimes another system change that database and the php script will return != 0. I want when that php script return != 0 to open a popup.

 

In short:

User looks the web page, AJAX runs automatically a php script, that script returns 0 and nothing happens. This AJAX is still running (like with window.setInterval()) and 5 minutes later that php returns != 0. Now a popup popups automatically on the user browser with "Hi luser, the result of the php script has been changed!"  :)

 

How to do that?

Thanks

Link to comment
Share on other sites

couple different options here.

 

#1) Interval - If you don't care about getting the notification immediately, this is definitely the easiest. You just execute the ajax command every set interval (you mentioned 5 minutes).

 

#2) Long Poll - If you want a more immediate response, and new information will happen fairly frequently (less then a couple of minutes), this is an easy method. What happens is the ajax command is executed, and the PHP script stalls with an infinite loop, checking frequently (like every half second) for more info. once info is found, return it. the javascript receives the info, the connection is closed, and then a new ajax call is opened immediately.

 

#3) Comit - This is something like Gmail, where there is a persistent two way connection between client and server. http://orbited.org/ is a good example of this.

 

...so...you said every 5 minutes, so i will assume option one. first note, is there is no need to test for something non-zero, then make another ajax call. your ajax call should return nothing or it should return the data you want. also, with ajax, it is much easier to use a js library (in this example i will use jQuery).

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
<script type="text/javascript">
  function checkForData ( ) {
    jQuery.get('data.php',false,function(data){
      if(data.length){
        //Do something with the data
        document.getElementById('my_response').innerHTML += "Found data: " + data + "\n";
      }else{
        //This is just so we can watch it work
        document.getElementById('my_response').innerHTML += "No data found\n";
      }
    });
  }
  //Start the timer when the page is done loading:
  $(function(){
    //Run it immediately
    checkForData();
    //Start timer
    setInterval('checkForData()',5000); //I used 5 seconds for testing
  });
</script>
<pre id="my_response"></pre>

<?php
if(rand(0,4) == 2)
  print "HERE IS SOME DATA";
?>

Link to comment
Share on other sites

Thanks for the reply, rhodesa

 

This is for a CRM system integrated with VoIP server. The goal is to popup a window to the user, logget in the CRM system with details about the caller (who has a detailed record in that CRM system). The delay can be 1-2 seconds max.

 

Opera has some issue with setInterval(). Looping php script is browser independent, but is it a good workaround to loop that script for the time the user is logged in? Maybe hours. And what will happen if a user just close his browser. The php script won't stop.

 

Orbited sounds good. I'll read some docs.

Link to comment
Share on other sites

Thanks for the reply, rhodesa

 

This is for a CRM system integrated with VoIP server. The goal is to popup a window to the user, logget in the CRM system with details about the caller (who has a detailed record in that CRM system). The delay can be 1-2 seconds max.

 

Opera has some issue with setInterval(). Looping php script is browser independent, but is it a good workaround to loop that script for the time the user is logged in? Maybe hours. And what will happen if a user just close his browser. The php script won't stop.

 

Orbited sounds good. I'll read some docs.

 

with the LONG POLL method, you can always close the PHP script after 30 seconds. the javascript will see there is no new info, and open another connection. that way you don't have to worry about script timeouts or people closing their browsers

Link to comment
Share on other sites

  • 1 month later...

Hi rhodesa

 

This piece of code is perfect which checks through data.php if there is any update lets say after 2 sec in this case. can you please let me know 1 thing.

if you put this code into test and see

 

it keeps adding a line

no data found

no data found

no data found

Found data etc etc

no data found

no data found

 

What i would like and I appreciate your help in this is following

 

in data.php

run a query which gets a total record of data on lets say user = 1234

 

so in 1 check for user = 1234 under mail table its == 1 record

 

so on the main page it would say

 

inbox (1)

 

now 2nd check after like 5 mins there is another record inserted for user = 1234

 

so on main page it would say

 

inbox (2)

 

and so on so forth. but not like it would add 2 enteries or 3 or 4 enteries on the main page like

 

inbox (1)

inbox (2)

inbox (3)

 

instead it would just update the same 1 display 1 to 2 to 3

 

please assist.

 

thank you

Link to comment
Share on other sites

well, instead of appending to the HTML node...replace it:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
<script type="text/javascript">
  function checkForData ( ) {
    jQuery.get('data.php',false,function(data){
      if(data.length){
        //Do something with the data
        document.getElementById('inbox').innerHTML = "Inbox (" + data + ")";
      }else{
        //This is just so we can watch it work
        //When there is no data, just ignore it
        //document.getElementById('my_response').innerHTML += "No data found\n";
      }
    });
  }
  //Start the timer when the page is done loading:
  $(function(){
    //Run it immediately
    checkForData();
    //Start timer
    setInterval('checkForData()',5000); //I used 5 seconds for testing
  });
</script>
<span id="inbox"></span>

 

you may want to look into JSON. it will allow you to pass your data in a structured way.

 

edit: fixed something

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.