2

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 Answer 1

1

The script code should like this

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

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

});
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you, but this code just to display data every one second and I want to modify just the modified values in my table.
it relods the div in each 1 second so the values get modified autometically.
Yes, but I do not want reload the data that are unchanged, I want to load only the values ​​that are modified not all the table. It's possible ?
In that case you will have to return JSON format values. The html markup would be in "index.php" file. We can call the php file that return the json data and parse this to index.php via ajax.
Thank you again. Could you simplify your response? Because I am beginner with these two languages.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.