Script run again if database has changes

can someone help me how to make my script run automatically again only if the database has some changes ....... in my current script it runs all the time according to var timer setIntervalto automatically update the output textboxes, if I change the value in the database without refreshing the page or click the button again to execute the script.

I want to do this, it will be executed again only if there are any changes in the value in the database ... does anyone know how to do this?

script code:

<script>
$(document).ready(function(){
var timer ;
$('#send_search_form').click(function(event){
        event.preventDefault();
        $(".search_form_input").val('');
        $(".empty_batchcode").html("Doesn't exist!");
        clearInterval(timer);
        updateTextboxes();

});
var $funiq_id         = $('#funiq_id'),
    $t_region         = $('#t_region'),
    $t_town           = $('#t_town'),
    $t_uniq_id        = $('#t_uniq_id'),
    $t_position       = $('#t_position'),
    $t_salary_grade   = $('#t_salary_grade'),
    $t_salary         = $('#t_salary');

function updateTextboxes(){
        $.ajax({
        url:"search.php",
        type:"GET",
        data: { term : $('#query').val() },
        dataType:"JSON",
        success: function(result) {

        var ii = 1;

        for (var i = 0; i < result.length; i++) { 
                    $funiq_id.html(result[i].value).show(); // reference
                    $t_region.val(result[i].region).show(); // reference
                    $t_town.val(result[i].town).show(); // reference
                    $t_uniq_id.val(result[i].uniq_id).show(); // reference
                    $t_position.val(result[i].position).show(); // reference
                    $t_salary_grade.val(result[i].salary_grade).show(); // reference
                    $t_salary.val(result[i].salary).show(); // reference
                    $('#id'+ii+'').val(result[i].atid).show();
                    $('#aic'+ii+'').val(result[i].atic).show();
                    $('#name'+ii+'').val(result[i].atname).show();
                    $('#other_qual'+ii+'').val(result[i].other_sum).show();
                    $('#interview'+ii+'').val(result[i].interview_sum).show();
            ii++;
            }


        if(timer == 1){

            timer = setTimeout(updateTextboxes,1000); 
        }

        }


    });

   timer = setInterval(updateTextboxes,50000);
}
});      
 </script>

search.php code:

<?php

if (isset($_GET['term'])) {

    $q = $_GET['term'];
    mysql_connect("localhost", "root", "");
    mysql_select_db("klayton");
    $query = mysql_query
("
SELECT DISTINCT 
ROUND((SELECT SUM(t2.inttotal)
 FROM app_interview2 AS t2 
 WHERE t2.atic = t.atic)/7,1)
 AS interview_sum,

ROUND((SELECT SUM(o2.ototal)
 FROM other_app2 AS o2 
 WHERE o2.oaic = t.atic)/7,1)
 AS other_sum,

atid,
atic,
atname,
region,
town,
uniq_id,
position,
salary_grade,
salary
FROM app_interview2 AS t
WHERE uniq_id = '$q'
GROUP BY t.atname HAVING COUNT(DISTINCT t.atic) ");

    $data = array();

    while ($row = mysql_fetch_array($query)) {
        $data[] = array(
            'value' => $row['uniq_id'],
            'atid' => $row['atid'],
            'atic' => $row['atic'],
            'region' => $row['region'],
            'town' => $row['town'],
            'uniq_id' => $row['uniq_id'],
            'position' => $row['position'],
            'salary_grade' => $row['salary_grade'],
            'salary' => $row['salary'],
            'atname' => $row['atname'],
            'other_sum' => $row['other_sum'],
            'interview_sum' => $row['interview_sum']
        );
    }

    header('Content-type: application/json');
    echo json_encode($data);

}

?>
+3
source share
1 answer

, . ( : , .)

Ajax

, ajax . , ajax, . ! , , . , (, ), ?

. , . , ajax , PHP, SQL-:

SELECT * FROM table ORDER BY `id` DESC LIMIT 1

PHP id. timestamp . , timestamp, id.

PHP (psuedocode)

// Settings
$oldid = $_GET['oldid'];
$delay = 2; // in seconds
$timeout = 20; // in seconds, must be below server and browser timeout.

// Check until different, or timeout.
$timer = 0;
while (1) {
  // TODO: Execute query and populate $id

  // If different, return as JSON.
  if ($id!=$oldid) {
    die('{"ajax": {"poll": "OK","id": '.$id.',"timestamp": '.time().'}');
  }

  // If taking to long, stop.
  if ($timer>$timeout) { die('{"ajax": {"poll": "timeout"}}'); }
  $timer += $delay;

  // The artificial delay.
  sleep($delay);
}

, somefile.php?oldid=3, DB 3, timeout 20 . id .

, AJAX, AJAX , . setinterval(), settimeout(). timeout, .

! , , CRC, timestamp , , .

WebSockets

- , . , -, .

0

All Articles