PHP:将输入存储在.txt文件中并输出到 html 页面


PHP: Store input in .txt file and output to html page

使用PHP,我想将来自html页面的用户输入存储在.txt文件中,并将该用户输入输出到同一页面上。

但是,.txt文件或页面div 中均未显示任何内容。

另外:我想让每条新消息显示在新的 DIV 上,我知道这是一个不同的问题,但我认为这可能是相关的。

关于为什么这不起作用的任何想法?

.HTML:

表单本身:

<textarea name="msg" rows="5"></textarea>
<form id="post" name="post" action="storeText.php" method="post">
    <input class="button" type="submit" value="POST!"/>
</form>

我想在同一个 html 页面上输出的地方:

<div class="menuItem" >
    <?=$msg?>
</div>

.PHP:

存储文本.php

<?php       
$filename = 'posts.txt';
$msg = (isset($_POST['msg']) ? $_POST['msg'] : null);
    // Let's make sure the file exists and is writable first.
    if (is_writable($filename)) {
    // In our example we're opening $filename in append mode.
    // The file pointer is at the bottom of the file hence
    // that's where $somecontent will go when we fwrite() it.
    if (!$handle = fopen($filename, 'a')) {
        echo "Cannot open file ($filename)";
        exit;
    }
    // Write $somecontent to our opened file.
    if (fwrite($handle, $msg) === FALSE) {
        echo "Cannot write to file ($filename)";
        exit;
    }
    echo "Success, wrote ($msg) to file ($filename)";
    fclose($handle);
    } else {
    echo "The file $filename is not writable";
    }
?>

试一试。

有两个文件,一个用于从同一页面发布的内容,DIV包含display_div.php

它的设置方式是它将在顶部显示最新帖子。

PHP 处理程序和表单

<?php       
// check if the file first exists, if not create the file
if (!file_exists("posts.txt")) {
$fp = fopen('posts.txt', 'a+');
fwrite($fp, '');
chmod("posts.txt", 0644);
// you may have to use this one
// remember to comment out the one with 0644 if using this one
// chmod("posts.txt", 0777);
fclose($fp);
}
// check if "msg" is set
if(isset($_REQUEST['msg'])) {
$file = 'posts.txt';  
$str = $_POST['msg'] . "'n";
$temp = file_get_contents($file);
$content = $str.$temp;
file_put_contents($file, $content);
// echo success message
echo "Saved to $file successfully!";
echo "<br>";
echo "Previous messages:";
echo "<hr>";
// print out previous posts
$file = file_get_contents('posts.txt', true);
$lines = explode(PHP_EOL, $file);
foreach ($lines as $line) {
    if (strpos($line, '/') === false) {
        $line = htmlspecialchars($line . "'n");
        echo nl2br($line);
    }
}
exit;
} // end of if(isset($_REQUEST['msg']))
?>
<form id="post" name="post" action="" method="post">
<textarea name="msg" rows="5"></textarea>
<br>
<input class="button" type="submit" name="submit" value="POST!" />
</form>
<?php include 'display_div.php'; ?>

display_div.php

注意:你可以去掉Previous messages:<br>或用你的文本替换它。

<div class="menuItem" >
Previous messages:<br>
<?php
$file = file_get_contents('posts.txt', true);
$lines = explode(PHP_EOL, $file);
foreach ($lines as $line) {
    if (strpos($line, '/') === false) {
        $line = htmlspecialchars($line . "'n");
        echo nl2br($line);
    }
}
?>
</div>

将 HTML 标记更改为以下内容:

<form id="post" name="post" action="storeText.php" method="post">
    <textarea cols="x" rows="x" name="msg"></textarea>
    <input class="button" type="submit" value="POST!"/>
</form>

您现在应该能够将数据写入文件,前提是该文件具有正确的权限并且 PHP 具有写入访问权限。

您似乎缺少输入。请看下文

<form id="post" name="post" action="storeText.php" method="post">
<textarea name="msg" cols="" rows=""></textarea>
<input class="button" type="submit" value="POST!"/>
</form>

谢谢