问题描述
我试图保持对我编写的表格的所有响应的总计,但是我很难使每个响应都换行。我下面有我的代码。我只希望它更易于阅读,因为现在发生的是所有响应都卡在了一起,希望每个响应都换一个新行。我尝试了几件事,并在代码中注释了它们,结果是什么。提前致谢。
<?PHP
if (isset($_POST[\'sometext\']))
{
$myFile = \"testFile.txt\";
$thetext=$_POST[\'sometext\'] ;//added + \"\\n\" here but all response turned to 0
writemyfile($myFile,$thetext,\"a\");
} else
{
$thetext=\"Enter text here\";
}
function readmyfile($thefile)
{
$file = fopen($thefile,\"r\") or exit(\"Unable to open file!\");
//Output a line of the file until the end is reached
while(!feof($file))
{
echo fgets($file). \"<br />\";
}
fclose($file);
}
function writemyfile($thefilename,$data,$mode)
{
$myfile=fopen($thefilename,$mode);
fwrite($myfile,$data); // added + \"\\n\" here and responses turned 0
fclose($myfile);
}
?>
<html>
<head>
<title> Zain\'s Test Site</title></head>
<body>
<form method=\"post\" action=\"<?PHP echo $PHP_self ?>\">
<input type=\"text\" name=\"sometext\" value=\"<?PHP echo $thetext ?>\" >
<input type=\"submit\" name=\"Submit\" value=\"Click this button\">
</form>
<?PHP readmyfile(\"testFile.txt\"); ?>
</body>
解决方法
您是否可以尝试将换行符(\\ n)附加到$ thetext变量,如下所示:
$thetext=$_POST[\'sometext\'] . \"\\n\";
请记住使用\'。\'作为连接运算符,并在换行符周围使用双引号。
,$thetext.\"\\n\"
在php中,您使用\“。\”连接字符串,而在javascript中使用\“ + \”。
,使用换行符\“ \\ n \”代替html的br \
,$text = $text.\"\\n\"
?
错误,请在此处输入更多文字以填写答案
, fwrite($myfile,$data); // added + \"\\n\" here and responses turned 0
concat字符串运算符是(。)而不是(+)
您也可以因此简化脚本
echo nl2br(get_file_contents($file));