0

i am trying to read data every three seconds without reloading the page. I am missing something in my script because it doesn't refresh data until i reload the page. Thanks in advance.

<html>
   <head>
       <meta charset="UTF-8">
       <title>Testing</title>       
      <!-- below script is for jquery --> 
       <script type="text/javascript" src="js/jquery-1.4.2.js"></script>
       <script type="text/javascript">
           function refreshData(){
               $.ajax({
                 url: 'localhost/index1.php',
                 type:"POST",
                 data:"dataPost", 
                 success: function(data){
                 document.getElementById("dataPost").innerHTML = data;

                   //render the dynamic data into html
                 }
               });
           }
           setInterval(refreshData, 3000);
           </script>
   </head>
   <div id="dataPost">
   <?php
       $conn = mysql_connect('localhost','user','password');    
       $db = mysql_select_db('db',$conn);
       $query = "select * from table";
       $result = mysql_query($query);
       while($row = mysql_fetch_array($result))
       {
           echo $row['data1']."<br/>";
       }
   ?>
   </div>    
</html>
7
  • 4
    Checked the console for errors? Commented Jan 24, 2014 at 21:15
  • i tried it doesnt work. console doesn't show any error, it just does not refresh data at specific interval Commented Jan 24, 2014 at 21:24
  • What does the network tab of the dev tools show? Commented Jan 24, 2014 at 21:26
  • 1
    What is the content in the index1.php? Commented Jan 24, 2014 at 21:26
  • Try var ref = setInterval(function(){refreshData()},3000); Commented Jan 24, 2014 at 21:39

3 Answers 3

2

I agree with @Axel but since Ajax is an Async function, even if your PHP code was in an external file, you would still have timing problems.

Basically, you can't know how long it takes for the Ajax call to complete (imagine the server being busy or down), so setting intervals manually to every 3 seconds make no sense. You need to set it to 3 seconds from the last completion.

On your php - just put your script in an external file (i.e script.php).

On your Javascript -

  1. Move the setInterval() inside the success event, and use setTimeout() instead.
  2. Create document.ready event and call the refreshData() from it.

      <script type="text/javascript">
        $(document).ready(function() {
        refreshData();
        });
                   function refreshData(){
                       $.ajax({
                         url: 'localhost/script.php',
                         type:"POST",
                         data:"dataPost",
                         success: function(data){
                         document.getElementById("dataPost").innerHTML = data;//why not use $('#dataPost').html(data) if you're already using jQuery?
                            setTimeout(refreshData, 3000);//will make the next call 3 seconds after the last one has finished.
                           //render the dynamic data into html
                         }
                       });
                   }
    
                   </script>
    

And your PHP file (script.php) will look like:

<?
       $conn = mysql_connect('localhost','user','password');    
       $db = mysql_select_db('db',$conn);
       $query = "select * from table";
       $result = mysql_query($query);
       while($row = mysql_fetch_array($result))
       {
           echo $row['data1']."<br/>";
       }
   ?>

Hope this helps.

P.S as many people will point out, mysql_connect is being deprecated so it will be best practice to start using MySQLi or PDO instead.

Sign up to request clarification or add additional context in comments.

2 Comments

it didnt work until i changed version number of javascript. But i agree with you that the way showed is the right way. +1 for that. Thanks.
the right answer is changing the version of javascript. Google chrome was not picking up old javascript so i changed to 1.8.0 version and it worked fine.
0

instead of calling the page itself, put the php code from out the div in to a single .php file and call this file within the AJAX call. that's all.

Comments

0

I found that the reason it was doing that is version of javascript. I used 1.8.0 and it started working fine. It was working whole time but google chrome had version issues. I did little research and tried changing version number and it worked.

1 Comment

version of jQuery please, not javascript

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.