我正在在线创建一个聊天室,想知道如何从index.php文件中获取输出并将其显示为新的p标签。 index.php从html文本框中获取用户输入,并将该短语添加到txt文件中;每次添加带有短语的新行。我很好奇如何检测到txt文件中已开始新行并创建带有该短语的单个p标签或小消息框。 (就像网上聊天一样)...
这是我的index.php:
<html>
<head>
<link href="https://fonts.googleapis.com/css?family=Lato" rel="stylesheet">
<title>Pretendr</title>
<font color='DCDCDC'>
<body bgcolor='233142'>
<form action="index.php" method="post">
<input type="text" name="chat" value="Type something here..."/>
<input type="submit" name="someAction" value="GO" />
</form>
<?php
if($_SERVER['REQUEST_METHOD'] == "POST" and isset($_POST['someAction']))
{
func();
}
function func()
{
$txt = $_POST['chat'];
$myfile = file_put_contents('chatData.txt', $txt.PHP_EOL , FILE_APPEND | LOCK_EX);
}
?>
<div><p><?php include('chatData.txt'); ?></p></div>
</body>
</font>
</head>
</html>
这是我的文本输出文件“ chatData.txt”:
Hello World!
It's quite lonely here... :(
I wish this text wasn't smashed together!
但是它显示如下:
Hello World! It's quite lonely here... :( I wish this text wasn't smashed together!
谢谢!