Automatic refresh table without refreshing PHP MySQL page

I have a very simple chat system that I created with PHP and MySQL (this is my second day ever using these languages) and I am wondering if there is a way to automatically update the table data that I pull from my database and loading into html table via PHP, not having something like Javascript go and reload the whole web page ... just reload the html table with the data it contains that PHP has filled in ... Does this make sense?

Here is my code if it helps (for / chat.php)

<html><head></head><body><center>
<form action="chat.php" method="post">
Message: <br><textarea type="text" name="message" style="width:80%; height:300px;"></textarea><br>
<input type="submit" name="submitButton"/> <a href="http://www.****.com/chat.php"><button name="Refresh Chat">Refresh Chat</button></a>
</form>
<div style="width:100%;">

<?php

$host="****";
$user="****";
$password="****";

$cxn = mysql_pconnect ($host, $user, $password);

mysql_select_db("defaultdb", $cxn);

if (getenv(HTTP_X_FORWARDED_FOR)) {
    $ipaddress = getenv(HTTP_X_FORWARDED_FOR);
} else {
    $ipaddress = getenv(REMOTE_ADDR);
}

$message = nl2br(strip_tags(nl2br($_POST["message"])));

if (isset($_POST['submitButton'])) { 
    if ($message != "") {
        mysql_query("INSERT INTO ChatTest (ID, TimeStamp, Message) VALUES ('$ipaddress', NOW(), '$message')");
    }
    header('Location: chat.php');
}

$message = "";

$data = mysql_query("SELECT * FROM ChatTest ORDER BY TimeStamp DESC") or die(mysql_error()); 
 Print "<table border cellpadding=3 width='100%' style='table-layout:fixed'>
        "; 
 Print "<tr>"; 
 Print "<th style='width:10%;'>ID:</th><th style='width:10%;'>TimeStamp:</th><th style='width:70%;'>Message:</th>";
 while($info = mysql_fetch_array( $data )) { 
 Print "
        <tr>"; 
    Print " <td>".$info['ID'] . "</td> "; 
    Print " <td>".$info['TimeStamp'] . " </td>";
    Print " <td style='white-space:pre-wrap;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;word-wrap:break-word'>".$info['Message'] . "</td></tr>
            "; 
 } 
 Print "</table>"; 

mysql_close($cxn);


?>

</div></center></body></html>

      

+2


source to share


2 answers


This method is called AJAX and one of the simplest libraries to add to a project would be jQuery . I am assuming your problem is not with JavaScript, but with the idea of ​​reloading the entire page.

UPDATE Because I'm such a nice guy;) This should work, more or less, I haven't tried it, so there might be a typo or two:



<?php

$host="****";
$user="****";
$password="****";

$cxn = mysql_pconnect ($host, $user, $password);

mysql_select_db("defaultdb", $cxn);

if (getenv(HTTP_X_FORWARDED_FOR)) {
$ipaddress = getenv(HTTP_X_FORWARDED_FOR);
} else {
$ipaddress = getenv(REMOTE_ADDR);
}

$message = nl2br(strip_tags(nl2br($_POST["message"])));
if (isset($_POST['submitButton'])) { 
if ($message != "") {
mysql_query("INSERT INTO ChatTest (ID, TimeStamp, Message) VALUES ('$ipaddress', NOW(), '$message')");
}
header('Location: chat.php');
}

$message = "";

$data = mysql_query("SELECT * FROM ChatTest ORDER BY TimeStamp DESC") or die(mysql_error()); 

$tbl = '';
$tbl .= "<table border cellpadding=3 width='100%' style='table-layout:fixed'>
"; 
$tbl .= "<tr>"; 
$tbl .= "<th style='width:10%;'>ID:</th><th style='width:10%;'>TimeStamp:</th><th style='width:70%;'>Message:</th>";
while($info = mysql_fetch_array( $data )) { 
$tbl .= "
<tr>"; 
$tbl .= " <td>".$info['ID'] . "</td> "; 
$tbl .= " <td>".$info['TimeStamp'] . " </td>";
$tbl .= " <td style='white-space:pre-wrap;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;word-wrap:break-word'>".$info['Message'] . "</td></tr>
"; 
} 
$tbl .= "</table>"; 

mysql_close($cxn);

if (isset ($_GET['update']))
{
    echo $tbl;
    die ();
}

?>
<html><head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js" type="text/javascript"></script>
</head><body><center>
<form action="chat.php" method="post">
Message: <br><textarea type="text" name="message" style="width:80%; height:300px;"></textarea><br>
<input type="submit" name="submitButton"/> <a href="http://www.****.com/chat.php"><button name="Refresh Chat">Refresh Chat</button></a>
</form>
<div id="messages" style="width:100%;">
  <?php echo $tbl; ?>
</div></center>
<script type="text/javascript">
$(document).ready (function () {
    var updater = setTimeout (function () {
        $('div#messages').load ('chat.php', 'update=true');
    }, 1000);
});
</script>
</body></html>

      

As far as coding techniques go, you might want to look into SQL Injection and maybe write cleaner HTML, but I'm sure you will get there :)

+5


source


the only way to do it without javascript is using an iframe for the chat interface and meta update.



but why not use javascript?

+1


source







All Articles