如何使用AJAX和jQuery动态更新数组表? [英] How to update an array table dynamically with ajax and jquery?

查看:150
本文介绍了如何使用AJAX和jQuery动态更新数组表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个文件PHP(gettable.php和的index.php)索引文件显示由gettable.php每一位seond,我要动态地在索引更新我的表的内容(仅适用于新的或变化值所得到的结果必须改变)用ajax,我在阿贾克斯初学者。

I have two files php (gettable.php and index.php) the index file display the result obtained by gettable.php every one seond, I want update the content of my table dynamically in the index (only new or changed value must be changed) using ajax, I am beginner in ajax.

请帮助我 谢谢

gettable.php

gettable.php

    //
    //
    // I load data from the server(xml file) 
    $xml = simplexml_load_string($result);

    foreach($xml as $node)
    {
        $name = "";
        $value = -1;

        foreach($node->attributes() as $a => $b) {
            if($a == "name")
            {
                $name = (string)$b;
    }
    else if($a == "value")
    {
        $value = (string)$b;
    }
        }

        $vars[$name] = $value;
    }

    ?>
    <table border="1">
    <tr>
    <th>id</th>
    <th>abc</th>
    <th>def</th>
    </tr>
    <tr>
    <td><?php
    echo "<p>x: ".$vars["x"]."</p>";
    ?>
    </td>
    <td><?php
    echo "<p>y: ".$vars["y"]."</p>";
    ?>
    </td>
    <td><?php
    echo "<p>z: ".$vars[z"]."</p>";
    ?>
    </td>
    </tr>
    </table> 

index.php文件

file index.php

    <html>  
<head>
    <title></title>
    <link rel="stylesheet" type="text/css" href="style.css" />
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
    <script language="JavaScript">
    $('#data').load("gettable.php");
    setInterval( "SANAjax();", 1000 ); 

    $(function() {
        SANAjax = function(){

        $('#data').load("gettable.php");

        }
    });


</script>
</head>
<body>

    <div id="data">
        <?php include_once('gettable.php'); ?>
    </div>

</body>

推荐答案

脚本code应该喜欢这样的

The script code should like this

$(function(){
  function loadData(){
    $('#data').load("gettable.php");
  }

  setInterval(function() { loadData(); }, 1000 ); 

});

这篇关于如何使用AJAX和jQuery动态更新数组表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆