javascript - 使用ajax无需刷新页面即可显示数据库记录

标签 javascript php jquery ajax callback

我正在尝试使用 ajax 函数显示数据库值。当我向数据库表中添加新行时,它不会立即显示记录。我必须刷新我的页面。然后只显示最近的日志详细信息。我认为我的 ajax 编码有问题。我是 Ajax 的新手。有人帮我解决我的问题..

function updateDriver(event)
{
    $(".panel a").each(function(){
        if($(this).hasClass("active"))
        $(this).removeClass("active");
        });


    $(this).addClass("active");
    ajaxObj.options.previousDriver = ajaxObj.options.data['did'];   
    ajaxObj.options.data = {'aid':'<?=$agent_id?>','did': event.data.did};

    //Ajax call for Driver Log Update;

    //function refreshEachMinute() {
      $("#RecentLog").html('Loading...');

      $.ajax({
              url: "<?=LOAD_LOG?>/",//The resource that delivers loc data.
              method: 'post', //data method
              dataType:'html',
              data: { aid: "<?=$agent_id?>", did: event.data.did },
              success: function(data)
              {
                  $('#RecentLog').html(data);
              },
              error: function()
              {
                  $('#RecentLog').html('<p>No Entries</p>')
              }

      });
    //}
    //setInterval(refreshEachMinute, 200);

}

PHP

case LOAD_LOG:

    if(!isset($_POST['aid']))
    die();

            $agent_id = $_POST['aid'];
            $driver_id = $_POST['did'];

            $sql = "SELECT * FROM ".TBL_DRIVER_LOG." where driver_id='$driver_id' ORDER BY id DESC";
            $log_data = asort_result_array($sql);
            $driver_log ="";
            if(count($log_data))
            foreach($log_data as $log)
            {
                extract($log);

                $ago = TimeAgo($date_of_update);                
                echo '<p>
                <span>'.$ago.', @ '.$average_speed.'km/hr</span><br>
                <span style="font-size: 13px; font-weight: normal;">'.$current_place.'</span>
                </p>
                <hr class="tabhr">';

            }
            else
            echo "No Entries";



        exit();

最佳答案

数据库无法触发您的 JavaScript 函数。您必须每隔几个时间间隔从客户端执行此操作。您可以使用 window.setInterval 来实现。

window.setInterval(function(){
   //your ajax function.
}, 200);//every 200 milliseconds

关于javascript - 使用ajax无需刷新页面即可显示数据库记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23441879/

相关文章:

javascript - 选择任何其他选项 bootstrap 时取消选择默认选择

php - 从命令行执行获取 Apache Document Root(无浏览器)

php - 如何使用 codeigniter 检索 mysql 数据库中的第一个单词 20

javascript - insertTextAtCursor() 函数使 contenteditable 失去焦点

javascript - Cytoscape.js - 突出显示所选节点的所有路径

javascript - gmail和facebook如何在不改变整个HTML DOM的情况下改变URL路径?

javascript - NodeJS MongoDB 投影不工作

php - 如何为Elasticsearch索引标准化的SQL数据库

jquery - 如何让 selectize 插入选项的文本,而不仅仅是值?

php - 星级评定系统,类似于亚马逊