Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/244.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如果数据库发生更改,请再次执行脚本_Javascript_Php_Mysql_Ajax_Json - Fatal编程技术网

Javascript 如果数据库发生更改,请再次执行脚本

Javascript 如果数据库发生更改,请再次执行脚本,javascript,php,mysql,ajax,json,Javascript,Php,Mysql,Ajax,Json,有人能帮我怎么做吗?如果数据库只有一些更改,我的脚本只会自动再次执行…在我当前的脚本中,它一直根据var计时器执行,以便在我更改数据库中的值时自动更新文本框的输出不刷新页面或再次单击按钮执行脚本 我想做的是,只有当数据库中的表中有一些值更改时,它才会再次执行…有人知道怎么做吗 脚本代码: <script> $(document).ready(function(){ var timer ; $('#send_search_form').click(function(event){

有人能帮我怎么做吗?如果数据库只有一些更改,我的脚本只会自动再次执行…在我当前的脚本中,它一直根据var
计时器执行,以便在我更改数据库中的值时自动更新
文本框的输出不刷新页面或再次单击按钮执行脚本

我想做的是,只有当数据库中的表中有一些值更改时,它才会再次执行…有人知道怎么做吗

脚本代码:

<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>

$(文档).ready(函数(){
无功定时器;
$(“#发送搜索表单”)。单击(函数(事件){
event.preventDefault();
$(“.search\u form\u input”).val(“”);
$(“.empty_batchcode”).html(“不存在!”);
清除间隔(计时器);
updateTextBox();
});
变量$funiq_id=$('funiq_id'),
$t#U区域=$(“#t#U区域”),
$t#u town=$(“#t#u town”),
$t#u uniq_id=$('t#u uniq_id'),
$t#U位置=$(“#t#U位置”),
$t#U salary_grade=$('t#U salary_grade'),
$t#U salary=$('t#U salary');
函数updateTextBox(){
$.ajax({
url:“search.php”,
键入:“获取”,
数据:{term:$('#query').val()},
数据类型:“JSON”,
成功:功能(结果){
var ii=1;
对于(var i=0;i
search.php代码:

<?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);

}

?>

这很难,但你可以做到。(这就是思想和计算机的问题:答案总是肯定的,只是需要一段时间才能实现。)

Ajax长轮询 假设您能够让ajax检索结果并将其显示给用户。现在在理论上,如果再次执行ajax请求,应该会得到更新的结果。你甚至可以做一个循环!但是这对于流量和用户系统来说不是很有效。相反,为什么不让服务器尽可能长时间地延迟其回答,同时定期检查(比如每两秒钟检查一次)数据库是否给出不同的结果

这称为长轮询。不过搜索结果很复杂,所以让我们从一个更简单的角度来看这个问题。假设ajax调用想要找出最新/最新的数据库条目,因此它请求执行此SQL查询的PHP文件:

SELECT * FROM table ORDER BY `id` DESC LIMIT 1
PHP文件只返回
id
。让我们附加一个debug
时间戳来区分各个请求。如果一切顺利,您将只看到
id
发生变化时
时间戳的变化

现在PHP做了(psuedocode)

现在,如果您使用
somefile.php?oldid=3调用此函数,并且最新的DB条目也是3,那么您将在20秒内返回
timeout
。除非在此时间内
id
更改为其他值

现在,执行AJAX的客户端在第一个AJAX请求完成之前不应该启动下一个AJAX请求。所以不要使用
setinterval()
,而是使用
settimeout()
。如果回复为
timeout
,您可能不想更新页面内容

就这样! 搜索结果也是如此,但您应该研究生成哈希、CRC、时间戳或其他低CPU方法,以查看结果是否与新查询结果不同

网袋
当web套接字标准化并在所有浏览器中实现时,这可能是一个更好的选择。它基本上就像长时间轮询一样使用,超时时间要高得多,因此没有多大收益。

两个词不可能您需要a)在javascript层中存储当前状态的表示,b)使用ajax定期轮询PHP函数(或使用websocket侦听器或类似工具)和c)将您的民意测验结果与当前州代表进行比较。像这样的框架可以为您做一些这样的事情,但是您仍然需要一些机制来轮询和更新数据。。。您在dba中存储一个修改的时间戳,并在JS中比较这个时间戳。。。
// 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);
}