PHP 文本框重置

PHP Textbox Reset

所以我正在使用 PHP 创建一个在线聊天室并刷新聊天,有一个按钮,稍后它会被隐藏但是 javascript 每秒自动点击一次,但是有一个小问题,每次点击它都会重置文本框,这意味着用户必须重新启动他们的消息。这是一个演示 teamhaxor.netau.net/Stuff/Chat/chat.php.

这是代码:

<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>

如何让它不重置用户输入的文本?提前致谢。

文本框正在重置,因为您每次提交表单时都会刷新页面,这是您使用 jquery 代码进行的。

要解决此问题,您应该改为调用 AJAX。