简体   繁体   English

如何将消息写到使用平面文件PHP创建消息的同一文件中

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

First of all this is not my code, I just need it changed a little. 首先,这不是我的代码,我只需要对其进行一些更改。

I need to know how to write messages onto the same file where the user posts the message. 我需要知道如何将消息写到用户发布消息的同一文件中。

Here is one page where the user can post their message: 这是用户可以在其中发布消息的页面:

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

And here is the other that writes the message onto a PHP file: 这是另一个将消息写入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>

Pretty much I just want everything on one page, I got close to doing that but it won't let me write onto the same page. 我几乎只希望将所有内容都放在一页上,但是我几乎可以做到这一点,但是它不允许我在同一页上书写。 I'm sure it's possible I'm just nowhere near experienced enough. 我敢肯定,我可能没有足够的经验。 Please help! 请帮忙!

What you want is to put content of both files in a single file, but execute the content of the second file only if form has been submitted. 您想要的是将两个文件的内容放在一个文件中,但是仅在提交表单后才执行第二个文件的内容。

First of all, change the form's method value to POST and replace all references to $_GET with $_POST 首先,将表单的方法值更改为POST,并将对$ _GET的所有引用替换 $ _POST

Then create a single file containing 然后创建一个包含

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM