我需要使用txt文件或其他任何可以被动查询的内容来创建评论框,而不使用数据库服务器。因为我对PHP编程很新,所以第一个想法是使用文本文件。一般来说,为了达到这个目的,我能够逻辑思考的代码是:
<html>
<head></head>
<body>
<form method = "post">
<textarea name = "txt" cols = "25" rows = "5">
Place your comment here ...
</textarea><br><br>
<input type = "submit" value = "Submit" onclick = "<?php
$com = $_POST["txt"];
$file = fopen("inrg.txt", "a");
fwrite($file, "<br>");
for($i=0; $i <= strlen($com) - 1; $i++)
{
fwrite($file, $com[$i]);
if($i % 37 == 0 && $i != 0)
fwrite($file, "<br/>");
}
fwrite($file, "<br>------------------------------------------");
fclose($file);
?>">
<br>
</form>
<font face = "Times New Roman"><b><p>Textul introdus este: </p></b></font>
<font face = "Comic Sans MS" color = "red" size = "2" >
<?php
$file = fopen("inrg.txt", "r");
echo fread($file, filesize("inrg.txt"));
fclose($file);
?>
</font>
</body>
</html>
没什么好看的,它确实需要在美学方面做一些改进。事情是在我在评论框中提交内容后,它显示正确,但如果我在网络浏览器中重新加载,最后发布的评论会在我重新加载页面时再次发布。 另外,如果有一种方法可以让PHP首先将“将你的评论放在这里......”消失
答案 0 :(得分:1)
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Example document</title>
</head>
<body>
<?php
// the relative path to the file
$fname = "theFile.txt";
// read in the file if present
if(file_exists($fname)) $txt = file_get_contents($fname);
// if the user pushes the submit button
if(isset($_POST["txt"])){
$txt = $_POST["txt"]; // get the entered content
file_put_contents($fname,$txt); // write the content to the file
}
?>
<form method="post" action="#">
<textarea name = "txt" cols = "25" rows = "5">
<?php echo $txt; ?>
</textarea><br />
<input type="submit" name="submit" value="submit" />
</form>
</body>
</html>
脚本所在的文件夹必须是PHP(= WebServer)可写的。这个脚本很关键,没有针对跨侧脚本黑客攻击的安全性。关于换行可能存在问题。
答案 1 :(得分:0)
以下是将页面重定向到自身的一种方法:
<html>
<head></head>
<body>
<form method="post">
<textarea name="txt" cols="25" rows="5"></textarea>
<br><br> <input type="submit" value="Submit" name="submit" />
<?php
if ( isset( $_POST[ 'submit' ] ) ) {
$com = $_POST[ "txt" ];
$file = fopen( "inrg.txt", "a" );
fwrite( $file, "<br>" );
for ( $i = 0; $i <= strlen( $com ) - 1; $i++ ) {
fwrite( $file, $com[ $i ] );
if ( $i % 37 == 0 && $i != 0 ) fwrite( $file, "<br/>" );
}
fwrite( $file, "<br>------------------------------------------" );
fclose( $file );
echo '<script type="text/javascript">window.location ="";</script>'; // Add here
}
?>
<br>
</form>
<font face="Times New Roman"><b><p>Textul introdus este: </p></b></font>
<font face="Comic Sans MS" color="red" size="2">
<?php
if (file_exists("inrg.txt")) {
$file = fopen( "inrg.txt", "r" );
echo fread( $file, filesize( "inrg.txt" ) );
fclose( $file );
}
?>
</font>
</body>
</html>
答案 2 :(得分:0)
尝试一下:
anotherpage.php
<?php
if($_POST){ // is a message has been posted
$fp = fopen('inrg.txt', 'a'); // open the text file and append using the 'a'
fwrite($fp, $_POST['txt']."\n");
fclose($fp);
$message_message_to_user = "Message added to file. Wait a sec and you will be redirected.";
}
header("Location: theformpage.php"); // regardless of if there is a post - send the user back to the original page
exit();
?>
theformpage.php
<html>
<head>
</head>
<body>
<form method="post" action="anotherpage.php">
<textarea name = "txt" cols = "25" rows = "5">Place your comment here ...</textarea><br><br>
<input type = "submit" value="Submit">
<br>
</form>
<hr />
<?php
// this reads all of the lines of the file and spits them out on the screen
$fh = fopen('inrg.txt','r');
while ($line = fgets($fh)){
echo nl2br($line); // nl2br() converts new lines to <br>'s
}
fclose($fh);
?>
</body>
</html>
答案 3 :(得分:0)
让我们按部分进行:
1)评论会一遍又一遍地发布,因为您在重新加载时重新提交表单。为避免这种情况,您需要在发布后重定向页面。这项技术称为Redirect after Post
。你应该搜索它,这很有意思。
2)PHP无法使文本消失,因为PHP将在页面加载完成后完成执行。为此,您需要学习Javascript。我也在学习JavaScript,我认为这很困难,但是请参阅Simply Javascript一书,它会很好地向你介绍JS。
只是为了让您知道,您必须在评论框中收听“onClick”事件,然后调用将清除评论框的JS函数。
会是这样的:
... id="thisBoxId" onclick="cleartext('thisBoxID')" >Enter a comment here...<...
和JS:
function cleartext( myitem ) {
myitem.text.value = "";
}
当然,一旦完成,文本将永远不会回来,直到您重新加载页面。为了让它回来,您应该使用onLostFocus
事件来重新填充文本。