使用 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:
storeText.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";
}
?>