使用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";
}
?>
答案 0 :(得分:1)
将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具有写访问权限,您现在应该能够将数据写入文件。
答案 1 :(得分:1)
有两个文件,一个用于从同一页面发布的提交,而DIV
包含display_div.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'; ?>
N.B。:您可以删除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>
答案 2 :(得分:0)
您似乎错过了一个输入。请参阅下面的
<form id="post" name="post" action="storeText.php" method="post">
<textarea name="msg" cols="" rows=""></textarea>
<input class="button" type="submit" value="POST!"/>
</form>
由于