0

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.

Woowing
  • 103
  • 1
  • 2
  • 15
  • Why do you need it to press the submit button? Just put it on a timer and have it happen automatically – Machavity Mar 13 '17 at 03:00
  • That's because when I tried to create a loop with a pause in PHP it wasn't working, I tried changing everything and it still didn't work so I did this. If you have figured it out please send some code for the loop. Thanks. – Woowing Mar 13 '17 at 03:04
  • I don't think you get the difference between client side and server side – Machavity Mar 13 '17 at 03:07
  • @Machavity I think that's why he's asking. Better help him out then or at least point him to the answer. – Lambasoft Mar 13 '17 at 03:09
  • @Lambasoft Your answer is really a comment. And I am pointing him to answers through the duplicate process. This question has been asked and answered many times – Machavity Mar 13 '17 at 03:15

1 Answers1

1

The textbox is resetting because you're refreshing the page every time you submit the form, which you're doing using your jquery code.

To fix that, you should make an AJAX call instead.

Lambasoft
  • 909
  • 2
  • 14
  • 33
  • How do I do that? Sorry if it's a noob question I am still learning PHP. – Woowing Mar 13 '17 at 03:08
  • Check this: http://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming The answer explains in details what's the difference between client and server. To send something from client to server using purely html, the page reloads everytime you do that, causing every textbox to reset. – Lambasoft Mar 13 '17 at 03:11