如何将消息写入使用平面文件 PHP s 创建它的同一个文件

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

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

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

这是一个页面,用户可以在其中 post 他们的消息:

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