所以我正在使用 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 调用。
关于javascript - PHP 文本框重置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42755963/