数据库记录显示没有使用ajax刷新页面

时间:2014-05-03 08:13:04

标签: 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();

3 个答案:

答案 0 :(得分:1)

数据库无法触发您的javascript函数。你必须每隔几个间隔从客户端做到这一点。您可以使用window.setInterval

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

答案 1 :(得分:0)

看起来您正试图从同一页面执行此操作?尝试将PHP分成不同的文件。当你附加LOAD_LOG查询字符串然后刷新页面时,你说它只显示那些条目,所以这告诉我你的case语句基本上是在写整个页面。

如果您创建一个单独的PHP文件,比如说“loadlog.php”,那么您可能会取得更大的成功,然后您可以使用/ path / to / loading.php替换ajax中的url选项。

答案 2 :(得分:0)

除了besabestin所说的......

如果你想让它只执行一次,你可以使用setTimeout而不是setInterval

  window.setInterval(function(){
   //your ajax function.
}, 200);
相关问题