【问题标题】:How can I write a message onto the same file that creates it with flat-file PHP s如何将消息写入使用平面文件 PHP 创建消息的同一文件中
【发布时间】:2015-09-22 08:02:04
【问题描述】:

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

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

这是用户可以发布消息的页面:

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

几乎我只想将所有内容都放在一页上,我已经接近做到这一点,但它不会让我在同一页上写字。我敢肯定,我可能还远远不够有经验。请帮忙!

【问题讨论】:

    标签: php html flat-file


    【解决方案1】:

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

    首先,将表单的方法值更改为 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>
    

    【讨论】:

      猜你喜欢
      • 2010-09-28
      • 2020-01-02
      • 1970-01-01
      • 2020-11-18
      • 1970-01-01
      • 1970-01-01
      • 2015-08-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多