PHP写入文本文件每次更新都会使文本删除一行



我正在使用一个非常基本的客户端/案例管理工具。其中一部分涉及使用PHP读/写文本的文本框。

我遇到的问题是,每次您单击"提交"文本时,文本都会下降一行。当您第一次在文本框中写入一些文本时,显然您将其写入顶行,但是当您单击时,提交时会在文本框中放下一行。奇怪的是,这是第一次发生(因此文本向下显示一行(时,.txt文件实际上在第一行上显示了文本(尽管它在文本框中的第2行中显示(。但是,如果您再次单击提交(例如,如果在文本框中添加/更改信息,甚至只是单击单击提交(,则文本会在文本框中删除另一行,这确实使其下降了.txt文件。

我想念什么吗?有什么方法可以阻止这种情况发生吗?

这是我在页面上使用的代码显示文本框:

<form action=".write-case-notes.php" method='post'>
<textarea name='textblock' style="width: 490px; height: 170px; resize: 
none;" >
<?php echo file_get_contents( ".case-notes.txt" ); ?>
</textarea>
<input type='submit' value='Update Case Notes'>
</form>

这是我用来写入.txt文件的PHP代码:

<?php
$f = fopen(".case-notes.txt", "w");
fwrite($f, $_POST["textblock"]); 
$newURL = '.parties.php';
header('Location: '.$newURL);
?>

提交新文本后,上述代码的$ newurl部分是将页面返回页面。

您实际上您自己的代码中的空白行:

<form action=".write-case-notes.php" method='post'>
<textarea name='textblock' style="width: 490px; height: 170px; resize: none;" >
<?php echo file_get_contents( ".case-notes.txt" ); ?>
</textarea>
<input type='submit' value='Update Case Notes'>
</form>

所以,当渲染HTML时,您...

  1. 首先,打开您的<textarea ...>标签。
  2. 然后写一个空白行 inceent textarea。
  3. 然后丢弃文件内容。
  4. 您实际上在它之后倾倒了另一个空白行。
  5. 然后您关闭textarea标签。

解决方案1(快速&amp; dirty(

消除空白行空格从您的HTML出来的新行:

<form action=".write-case-notes.php" method='post'>
<textarea name='textblock' style="width: 490px; height: 170px; resize: none;" ><?php echo file_get_contents( ".case-notes.txt" ); ?></textarea>
<input type='submit' value='Update Case Notes'>
</form>

解决方案2(有点清洁代码(

如果您想要更多的干净代码:

  1. 将您的内容分配到变量中。
  2. 如果您不使用Smarty,Twig或其他任何模板引擎,请使用简单的PHP替换来模板。
<?php
    // Content to be substituted.
    $textAreaContent = file_get_contents( '.case-notes.txt' );
    $textAreaStyle = 'width: 490px; height: 170px; resize: none;';
    // Template to substitute in.
    $template =
'<form action=".write-case-notes.php" method="post">
    <textarea name="textblock" style="%s">%s</textarea>
    <input type="submit" value="Update Case Notes">
</form>';
    // Render
    $html = sprintf( $template, $textAreaStyle, $textAreaContent );
    echo( $html );

注意:

  • 我将Yout HTML中的单个引号更改为双引号。
  • 如果您打开<?php标签和从不关闭它,则100%确定您不会发送不必要的线终止器或空格。这在此处记录:http://php.net/manual/en/language.basic-syntax.phptags.php

文档说:

如果文件是纯PHP代码,则最好省略PHP关闭 在文件末尾标记。这样可以防止意外的空间或新 PHP关闭标签后添加线,这可能会导致不必要 效果是因为没有 程序员的意图是在此点发送任何输出 脚本。

  • 在2018年,认真考虑使用模板引擎。这些链接可以帮助:
    • twig -http://twig.sensiolabs.org/
    • Smarty -https://www.smarty.net/
    • 本文中的任何其他内容:http://acmeextension.com/best-templating-engine/

希望提供帮助!

您可以尝试执行以下操作:

$file = './case-notes.txt';
$f = fopen($file, 'a');
fwrite($f, isset($_POST['textblock'])."n");
fclose($f);
header('Location: ./parties.php');

编辑:使位置重定向更简单。编辑:下面的新解决方案

<form action="./write-case-notes.php" method='post'>
<textarea name='textblock' style="width: 490px; height: 170px; resize: 
none;" >
</textarea>
<input type='submit' value='Update Case Notes'>
</form>
<?php
if (isset($_POST['textblock'])) {
	$text_block = $_POST['textblock'];
	$file = './case-notes.txt';
$f = fopen($file, 'a');
fwrite($f, $text_block."rn");
fclose($f);
}
?>
<br /><br /><br />
<?php $output = file_get_contents( "./case-notes.txt" );
print $output."<br />"; ?>

最新更新