Solved

Polling in JQuery.. must be a better way?

Posted on 2011-09-21
3
280 Views
Last Modified: 2012-05-12
Hi,
I'm using Jquery to update a couple divs on a timer and all works well. My problem is in order to accomplish it I have to run separate php files to query the database for each div so it returns the proper data to display for that div.

Is there a way I can simply use one query, pass back all the data and have it populate the appropriate divs? My code I'm using is below:
 
<script>
 $(document).ready(function() {
 	 $("#contest_status").load("application/modules/account/scripts/contest_status.php?account_id=4");
 	 $("#contest_title").load("application/modules/account/scripts/contest_title.php?account_id=4");


   var refreshId = setInterval(function() {
      $("#contest_status").load('application/modules/account/scripts/contest_status.php?account_id=4&randval='+ Math.random());
      $("#contest_title").load('application/modules/account/scripts/contest_title.php?account_id=4&randval='+ Math.random());

   }, 6000);
   $.ajaxSetup({ cache: false });
});
</script>

Open in new window


Thanks for any help.
0
Comment
Question by:tjyoung
  • 2
3 Comments
 
LVL 40

Expert Comment

by:gurvinder372
ID: 36575764
Yes, see this get method
http://api.jquery.com/jQuery.get/

in the success handler, you can parse data and place it in appropriate div
0
 
LVL 1

Author Comment

by:tjyoung
ID: 36577135
Great, that looks like the right direction.
Can you give me an idea what the code would look like with a timer?
Not particularly good at this.
0
 
LVL 40

Accepted Solution

by:
gurvinder372 earned 500 total points
ID: 36577647
function getData()
{
  //ajax call
  $.get('ajax/test.html', function(data) {
     //loading data in various divs
    $('.result').html(data);
    //alert('Load was performed.');
    setTimeout("getData()", 5*1000); //calling this method again after 5 seconds
  })
}
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question