I made a chat program, which is working fine except now, I need to add Ajax to the program to refresh the chat log every one second. The AJAX is supposed to call the function to grab data from the database every second so that if another person sends a message it will show up onto the chat log without having the user refresh the page. The name of my file is index.php:
Here is Ajax I have so far, which is not working: This is inside the head tag.
<script>
$(document).ready(function()
{
new refreshChat();
});
function refreshChat()
{
var x = $.ajax({
type: "POST",
url: "index.php",
data: {action: 'refresh'},
async: false,
success: function(output) {
alert(output);
}
}).complete(function(){
setTimeout(function(){refreshChat();}, 1000);
}).responseText;
$('div.chat_data').html(feedback);
}
</script>
Now, here is my PHP function to get data from the database (NOTE: I have no issues connecting to my database, so the PHP function should not be a problem with my program:
function refresh()
{
global $dbc;
$query2 = "SELECT `name`, `message` FROM `chatApp`";
$run2 = $dbc->query($query2);
while($row = $run2->fetch_assoc())
{
echo "<p>" . $row['name'] . ": " . $row['message'] . "</p>";
}
}