Refreshing Google map until new data has been added to the database &lt;html&gt;
&lt;head&gt;
  &lt;title&gt;GPS Tracker&lt;/title&gt;
  &lt;link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.min.css"&gt;
  &lt;link rel="stylesheet" type="text/css" href="view/style.css"&gt;
  &lt;script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"&gt;/script&gt;
  &lt;script type="text/javascript"&gt;
    function show_maps()
      {
      var div_peta = document.getElementById('kanvas');
      var tengah = new google.maps.LatLng(-8.801502,115.174794);
      var options = 
      {
        center : tengah,
        zoom : 14,
        mapTypeId : google.maps.MapTypeId.ROADMAP
      }
      //make map object
      var google_map = new google.maps.Map(div_peta,options);
   }
 &lt;/script&gt;
&lt;/head&gt;
&lt;body OnLoad="show_maps();"&gt;
 &lt;div class="container"&gt;
 &lt;div class="row"&gt;
   &lt;div class="col-md-4"&gt;
     &lt;div class="panel panel-default panel-body"&gt;
       &lt;p&gt;&lt;b&gt;&lt;h4&gt;Tracking&lt;/h4&gt;&lt;/b&gt;
       &lt;hr&gt;
       &lt;?php
         include"controller/connection.php";
         $query=mysql_query("SELECT id_history FROM tb_history");
         $count=mysql_num_rows($query);
       ?&gt;
       &lt;form method="POST" action="&lt;?=$_SERVER['PHP_SELF']?&gt;"&gt;
         &lt;input type="hidden" name="count" value="&lt;?php echo $count ?&gt;"&gt;
         &lt;input type="submit" name="submit" class="btn btn-primary" value="Get Location" style="width:280px"&gt; &lt;/button&gt;&lt;/a&gt;&lt;/p&gt;
       &lt;/form&gt;
     &lt;/div&gt;
   &lt;/div&gt;
   &lt;div class="col-md-8"&gt;
     &lt;div class="panel panel-default panel-body" style="height:490px"&gt;
       &lt;div id="kanvas"&gt; kanvas peta&lt;/div&gt;
     &lt;/div&gt;
   &lt;/div&gt;
 &lt;/div&gt;
&lt;?php
 if(isset($_POST['submit']))
   {
     include "controller/connection.php";
     $count1 = $_POST['count']; 
     $count2 = $count1+1;
     $sql = mysql_query("SELECT id_history FROM tb_history");
     $count3 = mysql_num_rows($sql);
     if($count3==$count1)
     {
       echo"&lt;script&gt;setTimeout(show_maps,1000)&lt;/script&gt;";  
     }
     else if
     {
       echo"&lt;script type='text/javascript'&gt;
       alert('Data has been added!');
       &lt;/script&gt;";
     }
   }
 ?&gt;
&lt;/body&gt;
&lt;/html&gt;


The code that I wrote above will show a button and a Google map.

Once the button has been clicked, the code will send the amount of data that exist in the tb_history.

The PHP code at the bottom will always check if there's any new data added or not. If not, then it will refresh the Google map until new data has been added to the table tb_history.

Can anyone suggest a better algorithm for this?
In my opinion the best way to achieve what you want is to work with Event Listeners.

So basically, breaking it down to software design, you would fire a event if something is written to the database, so everytime you insert into the database. Maybe also when you update or delete something if you want to trigger these too.

Then you need an Event Listener which will run php code which runs and does what you want when new Data is added only when new Data is added.

Then you could send frequently requests to the PHP file, which gives you the message back if data is updated or not, but that would be counterproductive behavior because your already working event driven.

So you fire a Response to the JavaScript from the PHP file when the Event got fired. So your whole code will only run if it must, which depends only on if there's new data or not. Without continious requests whatsoever.

This would be the best attempt, you'll need some things for that, like the before mentioned Event Listeners, jQuery Ajax Calls or Plain JavaScript Ajax Calls.

Also some thougts to your SQL code

$count1 = $_POST['count']; //NEVER use data from Requests directly! 


You're vulnerable to SQL Injection! Always escape the input and use prepared statements

$count2 = $count1+1; //Attempting to the above code there's no need for this line


And at least you can mix these 2 lines togther to one line:

$sql = mysql_query("SELECT id_history FROM tb_history");
$count3 = mysql_num_rows($sql);


Would be

$count3 = mysql_query("SELECT count(id_history) FROM tb_history");