使用 PHP 将 textarea 的内容保存到 .txt 文件

高里·拉克希米

我有一个代码,它应该使用 PHP 将文本区域中的内容保存为文本文件,如果我单击按钮,则会创建文件,但其中没有写入数据。

代码:

           <form  method="POST" action="">
              <div data-label="Index.php" class="demo-code-preview" contentEditable="true">
                          <pre><code class="language-html">
                              <textarea name="content">
                            <?php
                              $file = "index.php";
                              echo(htmlspecialchars(file_get_contents($file)));
                              ?>
                            </textarea>
                          </code></pre>
              </div>
            </a>
              <a href="?SubmitFile=true" class="bar-anchor" name="SaveFile">
               <span>Save</span>
            <div class="transition-bar"></div>
            </a>
          </form>



<?php
if (isset($_GET['SubmitFile'])){
    $content = $_POST['content'];
    echo "<script>console.log('" . $content . "' );</script>";
    $file = "myfile.txt"; // cannot be an online resource
    $Saved_File = fopen($file, 'a+');
    fwrite($Saved_File, $content);
    fclose($Saved_File);
  }
?>
罗布·鲁切特

您实际上并未提交表单,只是在单击链接时将 SubmitFile 参数作为 GET 参数传递。最简单的方法是用提交按钮替换链接,然后更改 PHP 以在 POST 中查找该参数。

如果您确实需要使用链接来提交表单,则需要使用一些 javascript 来取消链接上的默认事件并提交表单。在这种情况下,您需要将 SubmitFile 参数添加为隐藏字段。

<form method="POST" action="">
    <div data-label="Index.php" class="demo-code-preview" contentEditable="true">
          <pre><code class="language-html">
              <textarea name="content">
            <?php
            $file = "index.php";
            echo(htmlspecialchars(file_get_contents($file)));
            ?>
            </textarea>
          </code></pre>
    </div>
    <!-- 
    This does not submit the form, it only makes a get request with the SubmitFile parameter.
    You need ot submit the form and send the SubmitFile in the post, or add the SubmitFile
    parameter to the form action if you really want to see it in the GET params.
       
    <a href="?SubmitFile=true" class="bar-anchor" name="SaveFile"><span>Save</span></a>
    -->
    <button type="submit" name="SubmitFile">Save</button>
</form>


<?php
/*
 * This will be true if the parameter is in the GET, but does not guarantee that
 * the form was posted, so you cannot rely on this as a GET parameter.
 * Change it to POST in the markup and here.
 * 
 * if (isset($_GET['SubmitFile']))
 */
if (isset($_POST['SubmitFile']))
{
    $content = $_POST['content'];
    echo "<script>console.log('" . $content . "' );</script>";
    $file       = "myfile.txt"; // cannot be an online resource
    $Saved_File = fopen($file, 'a+');
    fwrite($Saved_File, $content);
    fclose($Saved_File);
}
?>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将列表保存到.txt文件

将JList保存到Txt文件中

使用按钮将JTextArea保存到.txt文件

如何从csv读取不带标题的列,并使用Python将输出保存到txt文件中?

将richTextBox内容保存到C#上的.txt文件

使用NodeJS(或PHP)将API数据保存到JSON文件

将变量保存到PHP中的.txt文件?

将textarea数据从HTML页面保存到TXT文件

如何将php查询的所有结果保存到.txt文件?

将大.txt文件的内容保存到python中的变量中

将List <>保存到.txt文件中

使用PHP将文件保存到特定位置

如何将textview内容保存到.txt文件?

如何使用PHP将文本区域内容保存在.txt文件中

如何将值从textarea保存到空白文件?

如何将PHP网页的内容保存到XML文件?

将textarea的内容保存到文件并从PHP服务器页面加载

如何使用php5将html5表单的textarea内容保存到服务器端的文本文件?

将ListView保存到txt文件

如何使用Android Java将浮动内容保存到文件?

将结果保存到txt文件python

将终端输出保存到txt文件

如何将 EditText(纯文本)的内容保存到内部存储上的 Txt 文件中

Javascript将变量保存到服务器上的.txt文件并覆盖.txt内容

使用python3将html内容保存到txt文件中

使用php将html表单数据保存到txt文件

将 ping 结果保存到 TXT 文件

如何使用 php 将 echo 的所有变量结果保存到 txt 文件中?

使用 windows cmd 将所有提交差异保存到 txt 文件