I have created a chat window using php, ajax, and jquery. It successfully reads and writes to a file called chatlog.html. I have made a button that clears the chat. It works fine, but all of the clients' chats don't clear until someone speaks. How would I fix this?
chat.php is here since I can't format it correctly: http://pastebin.com/AEwjeZ3w
sendchatmsg.php:
<?php
session_start();
if (isset($_SESSION['username'])) {
$text = $_POST['cmsg'];
$fp = fopen("chatlog.html", "a");
fwrite($fp, "<div><b>" . $_SESSION['username'] . "</b>: " . stripslashes(htmlspecialchars($text)) . "<br></div>");
fclose($fp);
}
?>
clearchat.php:
<?php
unlink("chatlog.html");
?>
You can write empty file to it when you clear it.
clearchat.php:
$fp = fopen("chatlog.html", "w");
fwrite($fp, " ";
fclose($fp);
Reload the chat log in user's screen once the clear chat event completes.
$("#clearchat").click(function() {
$.post("clearchat.php",function(res){
loadLog();
});
});