javascript - Save textarea data from HTML page to a TXT file

575

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.

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.

I have tried this:

<textarea id="toroq" class="page_speed_1234354935"><?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

227

Answer

Solution:

JavaScript is not able to edit or make changes directly to files stored on the web server without having some form of code running.

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.

Change your HTML form to something like this:

<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. 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>
281

Answer

Solution:

You cant write directly from javascript. You have to use PHP to write. You can usefile_get_contents to get file content and you can usefile_get_contents to write. You can do something like below with HTML form.

<?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";
}

?>

People are also looking for solutions to the problem: Cannot get data in INPUT_GET in php

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.