简体   繁体   English

在php页面加载后使用Ajax动态呈现表值

[英]Render table values dynamically using Ajax after page load in php

My idea is to get connectivity status of available servers in database on a php page. 我的想法是在php页面上获取数据库中可用服务器的连接状态。

  <tbody>
  <?php foreach ($data['servers'] as $server) { ?>
     <tr>
          <td class=""><?php echo $server->server_name; ?></td>
          <td class=""><?php echo $server->base_path; ?></td>
          <td class="server_status"><?php if (is_dir($server->base_path)){ echo 'Pass';} else { echo 'Fail' } ?></td>
    </tr>
  <?php  } ?>
  </tbody>

I want to do this just after page load using ajax like this page screenshot 我想在使用ajax这样的页面截图加载页面之后立即执行此操作

How do I call ajax to get each value of this dynamically generated table. 我如何调用ajax来获取此动态生成的表的每个值。 So far, I have tried the following code 到目前为止,我已经尝试了以下代码

<?php foreach ($data['servers'] as $server) { ?>
     <tr>
          <td class=""><?php echo $server->server_name; ?></td>
          <td class=""><?php echo $server->base_path; ?></td>
          <td class="server_status"></td>
          <td class="server_status_loading"></td>
    </tr>
  <?php  } ?>

JS JS

$(function(){

$(".server_status").hide();
$(".server_status_loading").show();
$.ajax({
    url: 'get-server-status'
})
.error(function(){
    alert('Error!');
})
.done(function(response){

    $(".server_status_loading").hide();
    $(".server_status").show();
    $(".server_status").html(response);
});

get-server-status function: 获取服务器状态功能:

    public function getStatus() {

    $basep = $this->_serverModel->getBasePath(2);
    if (is_dir($basep)) { 
        echo 'Pass'; exit;
    } 
    else { 
        echo 'Fail'; exit;
    }
}

Thanks in advance! 提前致谢!

try like this 这样尝试

$(document).ready(function(){
    var hTable = document.getElementById('table2');
        var iRowCount = hTable.rows.length;

    for(var i=1; i<iRowCount; i++){
        basepath= hTable.rows[i].cells[2].childNodes[1].value;      
        $.ajax({
           url: 'get-server-status',
           data :basepath 
        })
    .error(function(){
        alert('Error!');
    })
    .done(function(response){
        hTable.rows[i].cells[2].childNodes[3].value=response;
    });

    }
}

first add id to your <tr> . 首先将id添加到您的<tr>

<?php foreach ($data['servers'] as $server) { ?>
 <tr id="echo database id here.">
      <td class=""><?php echo $server->server_name; ?></td>
      <td class=""><?php echo $server->base_path; ?></td>
      <td class="server_status"></td>
      <td class="server_status_loading"></td>
</tr>

then check this link to handle the ajax on page load and this link to handle the ajax on click.Rest everything is provided there. 然后检查链接以处理页面加载时的ajax,并单击链接以处理click上的ajax。在此提供所有内容。

In link you can replace status to server_status_loading 在链接中,您可以将status替换为server_status_loading

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM