简体   繁体   English

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

[英]Save textarea data from HTML page to a TXT file

I am building a database server for an organization that I am a member of, and one of the options is that they need to be able to change a list of names. 我正在为我所属的组织构建数据库服务器,其中一个选项是他们需要能够更改名称列表。 This list is in a txt file located in the webroot. 此列表位于webroot中的txt文件中。

I have loaded the txt file content in a textarea in an html page, and now what I need to be able to do is to save the textarea content to the same file, in case we need to update or change the names. 我已经将txt文件内容加载到html页面的textarea中,现在我需要做的是将textarea内容保存到同一个文件中,以防我们需要更新或更改名称。

I have tried this: 我试过这个:

<textarea id="toroq" style="width: 100%; height: 200px;"><?php include("lista_toroq.txt") ?></textarea>
<input id="save_toroq" type="button" value="Save" onclick="WriteToFile();"/>
function WriteToFile()
{
    var fso = new ActiveXObject("Scripting.FileSystemObject");
    var text=document.getElementById("toroq").innerText;
    //var fileName=document.getElementById("TextArea2").innerText;
    var s = fso.CreateTextFile("lista_toroq.txt", true);
    s.WriteLine(text);
    s.WriteLine('***********************');
    s.Close();
}

the problem is that it is not saving the file 问题是它没有保存文件

JavaScript is not able to edit or make changes directly to files stored on the web server without having some form of code running. 如果没有运行某种形式的代码,JavaScript无法直接编辑或更改存储在Web服务器上的文件。

Using something like this: 使用这样的东西:

<?php
if ($_POST["text"]) {
    file_put_contents("file.txt", $_POST["text"]);
}
?>

The above code runs when $_POST["text"] has been set. 上面的代码在设置$_POST["text"]时运行。

Change your HTML form to something like this: 将您的HTML表单更改为以下内容:

<html>
  <body>
    <form method="POST">
      <textarea name="text tyle="width: 100%; height: 200px;"></textarea>
      <input type="submit" value="submit" />
    </form>
  </body>
</html>

You can store both of these bits of code in one PHP file. 您可以将这两部分代码存储在一个PHP文件中。 The result would be like this: 结果将是这样的:

<?php

if (isset($_POST["text"])) {
    $filename = "lista_toroq.txt";
    $fileContent = file_get_contents ($filename);

    $separator = "***********************";
    $new_content = $_POST["text"].$separator.$fileContent

    file_put_contents($filename, $new_content);
    echo "Text added to file";
}

?>
<html>
  <body>
    <form method="POST">
      <textarea name="text" tyle="width: 100%; height: 200px;"></textarea>
      <input type="submit" value="submit" />
    </form>
  </body>
</html>

You cant write directly from javascript. 你不能直接从javascript写。 You have to use PHP to write. 你必须使用PHP来编写。 You can use file_get_contents to get file content and you can use file_get_contents to write. 您可以使用file_get_contents来获取文件内容,您可以使用file_get_contents来编写。 You can do something like below with HTML form. 您可以使用HTML表单执行以下操作。

<?php

if (isset($_POST["text"])) {
    $filename = "lista_toroq.txt";
    $fileContent = file_get_contents ($filename);

    $separator = "***********************";
    $new_content = $_POST["text"].$separator.$fileContent

    file_put_contents($filename, $new_content);
    echo "Text added to file";
}

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM