How to update an array table dynamically with ajax and jquery?

风流意气都作罢 提交于 2019-12-24 01:19:13

问题


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.

Help me please thank you

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> 

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>


回答1:


The script code should like this

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

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

});


来源:https://stackoverflow.com/questions/12184199/how-to-update-an-array-table-dynamically-with-ajax-and-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!