So I am making an online chatroom using PHP and to refresh the chat, there is a button, later on it will be hidden but javascript automatically clicks this every second, but there is one slight problem, every time it clicks it resets the textbox, meaning the user has to restart their message. Here is a demo teamhaxor.netau.net/Stuff/Chat/chat.php.
This is the code:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="submit" name="refresh" id="update">
</form>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="text" name="text" id="text">
<input type="submit" name="send">
</form>
<?php
if(isset($_POST['refresh'])) /* i.e. the PHP code is executed only when someone presses Submit button in the below given HTML Form */
{
if ($file = fopen("log.txt", "r")) {
while(!feof($file)) {
$line = fgets($file);
echo $line . "<br>";
}
fclose($file);
}
}
if(isset($_POST['send'])) /* i.e. the PHP code is executed only when someone presses Submit button in the below given HTML Form */
{
$text = $_POST['text'];
$myfile = fopen("log.txt", "a+") or die("Unable to open file!");
fwrite($myfile, "\n" . $text);
fclose($myfile);
}
?>
<script>
setInterval(function(){
element = document.getElementById('update');
element.click()
}, 1000);
</script>
How can I make it not reset the text the user is typing? Thanks in advance.