Autoupdating Webpage with PHP, jquery, and ajax
===

I am trying to create a webpage with an html table that autoupdates with data from a mysql database.  The html table also has the ability to update the mysql table by way of a form.  My problem is that I can't get the table to autoupdate.  I know my phptable.php script works, because I can `include` it and play with it, but it won't autoupdate.

I realize that I probably need to use jquery and ajax, but I just can't get it to work.  Can you guys please give me some help?

 - In my ajax call, do I need to pass any data?  I don't want to update anything, I just want to read the whole database.
 - In the ajax call, I've tried playing with `dataType: html` since I'm echoing html code in phptable.php, but I can't get that to work.  Is this the best way, or...
 - In my ajax call, would it be better to use `dataType: json` and have the phptable.php script return my database as json output?

Is there something else that I'm not thinking of?  Thanks a lot guys, I'm looking forward to your help!
 

**topphp.php**
        
    <?php
    
    // Simple check to make sure that the client
    // is from the local network 
    $clientip = getRealIpAddr();
    
    if (strpos($clientip,"27.0.0.1")==1)
            {
            echo "Authorized: local host";
            echo "<br/>";
            echo $clientip;
            }
    
    elseif (!strpos($clientip,"92.168.3."))
            {
            echo "Unauthorized User";
            echo "<br/>";
            echo $clientip;
            die();
            }
    else
            {
            echo "Authorized User";
            echo "<br/>";
            echo "<br/>";
            echo "The client's IP is ".$clientip.".<br/>";
        }
    include 'phptable.php';
    ?>
    
    <html>
    <head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs$
    </script>
    </head>
    <body>
    <script id="source" language="javascript" type="text/javascript">
    $.ajax({
     url:'phptable.php',
     type: 'POST',
     async: true,
     data: ''
    });
    </script>
    </body>
    </html>
    
    <?php
    function getRealIpAddr()
    {
        if (!empty($_SERVER['HTTP_CLIENT_IP']))   //check ip from share inte$
        {
          $ip=$_SERVER['HTTP_CLIENT_IP'];
        }
        elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //to check ip is$
        {
          $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
        }
        else
        {
          $ip=$_SERVER['REMOTE_ADDR'];
        }
        return $ip;
    }
    ?>



**phptable.php**

    <?php
    
    session_start();
    
    $MySQLUsername = "username";
    $MySQLPassword = "password";
    $MySQLHost = "localhost";
    $MySQLDB = "datadatabase";
    
    $con = mysql_connect($MySQLHost, $MySQLUsername, $MySQLPassword);
    if (!$con)
            {
            die('Could not connect: ' . mysql_error());
            }
    
    mysql_select_db($MySQLDB, $con);
    
    
    if (isset($_POST['action']))
            {
            if ($_POST['action'] == "setState")
                   {
                    $id = $_POST['id'];
                    if ($_POST['currentState']==1)
                            {
                            $newState = 0;
    //                      echo "New state is ".$newState;
                            }
                    elseif ($_POST['currentState']==0)
                            {
                            $newState = 1;
    //                      echo "New state is ".$newState;
                            }
                    mysql_query("UPDATE homeauto SET state=$newState WHERE id=$id");
                    mysql_query("UPDATE homeauto SET changedby='website' WHERE id=$id");
                    }
            }
    $result = mysql_query("SELECT * FROM homeauto");
    $num = mysql_num_rows($result);
    echo "<table border=\"1\">";
    echo "<tr>";
    echo "<th>Name</th>";
    echo "<th>IP</th>";
    echo "<th>State</th>";
    echo "<th>Change State</th>";
    echo "</tr>";
    for($i=0; $i<$num; $i++)
            {
            $name=mysql_result($result,$i,"name");
            $ip=mysql_result($result,$i,"ip");
            $state=mysql_result($result,$i,"state");
            $id=mysql_result($result,$i,"id");
            if ($state==0)
                    {
                    $buttonText = "TurnOn";
                    }
            elseif ($state==1)
                    {
                    $buttonText = "TurnOff";
                    }
            echo "<tr>";
            echo "<td>".$name."</td>";
            echo "<td>".$ip."</td>";
            echo "<td>".$state."</td>";
            echo "<td><form method=\"post\">
                   <input type=\"hidden\" name=\"action\" value=\"setState\">
                    <input type=\"hidden\" name=\"currentState\" value=$state>
                    <input type=\"hidden\" name=\"id\" value=$id>
                    <input type=\"submit\" value=$buttonText>
                    </form></td>";
            echo "</tr>";
            }
    
    mysql_close($con);
    echo "<br/>";
    
    ?>