I am making a chat room for my website. I am using php and mysql to display and store the messages. I am trying to use Jquery and AJAX to refresh the page every 5 seconds. I have never used Jquery or AJAX before. Here is my Jquery and AJAX code.
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
var auto_refresh = setInterval(
function(){
$('#message_display').load('show_messages_public.php');
}, 5000);
</script>
In the 'show_messages_public.php' page I have a function called 'show_messages()' how would I call this function from the AJAX and Jquery?
<?php
session_start();
function show_messages()
{
$mysql_host = "******";
$mysql_database = "*******";
$mysql_user = "*****";
$mysql_password = "*****";
$link= mysql_connect($mysql_host, $mysql_user, $mysql_password);
if (!$link) {
die('could not connect:' . mysql_error());
}
$db_selected= mysql_select_db($mysql_database);
if (!$db_selected) {
die('can\'t use' . $mysql_database . ':' . mysql_error());
}
$query= "SELECT * FROM public_chat ORDER BY time DESC LIMIT 2";
$result= mysql_query($query) or die(mysql_error());
while($row= mysql_fetch_array ($result)) {
echo '<br/><font color="black">' .$row['time'] . "<br>" . '<b><big>' . '<font color="black">' . $row['user'] . ':' . '</big></b>' . " <br> " . $row['message'] .'</font>';
echo "<br/><hr width=\"95%\"<br/>"/*"<hr width=\"90%\">"*/;
}
}
?>