如何在用平面文件PHP创建消息的同一个文件上写入消息


How can I write a message onto the same file that creates it with flat-file PHP s

首先,这不是我的代码,我只需要稍微修改一下。

我需要知道如何将消息写入用户发布消息的同一文件中。

这里有一个页面,用户可以发布他们的消息:

<html>
<head>
<title>Form to Flat File</title>
</head>
<body>
<form action="sendinfo.php" method="get">
Your Name:<br />
<input type="text" name="name"><br />
Your Message:<br />
<textarea name="message"></textarea><br />
<input type="submit" value="Send Info">
</form>
</body>
</html>

这是另一个将消息写入PHP文件的程序:

<html>
<head>
<title>Form to Flat File</title>
</head>
<body>
<?php 
include('config.php');
$user = $_GET["name"]; 
$message = $_GET["message"];
print("<b>Thank You!</b><br />Your information has been added! You can see it by <a href=savedinfo.php>Clicking Here</a>");
$out = fopen("savedinfo.php", "a"); 
if (!$out) { 
print("Could not append to file"); 
exit; 
} 
fputs ($out,implode,("'n")); 
fwrite($out,"<b>$user</b><br />$message<br /><br />");
fclose($out);
?>
</body>
</html>

我几乎只想把所有的东西都写在一页上,我差点就做到了,但它不允许我写在同一页上。我相信我可能只是经验不足。请帮忙!

您想要的是将两个文件的内容放在一个文件中,但只有在表单已提交的情况下才执行第二个文件的属性。

首先,将表单的方法值更改为POST,并将对$_GET的所有引用替换为$POST

然后创建一个包含的单个文件

<html>
<head>
<title>Form to Flat File</title>
</head>
<body>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// here put content of the file that stores form values in a file
// just remove all html code and leave only PHP code
}

?>
// here put content of the file that displays the form
// just remove HTML, HEAD and BODY tags first
</body>
</html>