1

I'm trying to find a way to post content onto another .txt file that I host. I know there is a way to load data, but is there a way to save it?

For loading, I have:

<!doctype html>
<html>
<body>
<script src=/files/scripts/jquery.js></script><!--i host jquery-->
<script>
jQuery.get('/development/lol.txt',function(data){
  $('body').text(data);
});
</script>
</body>
</html>

But is there a way I can write data onto a .txt file like how I loaded it?

enigma
  • 11
  • 1
  • 4
    `txt` file is a static resource. Must use a server side dynamic programming language to write to file – charlietfl Jan 31 '17 at 02:23
  • You need to write server side code to accomplish this. – Mauro Aguilar Jan 31 '17 at 02:23
  • I would use an Ajax POST request to write to the file. – D. Foley Jan 31 '17 at 02:30
  • 2
    @D.Foley that's not exactly helpful – Phil Jan 31 '17 at 02:33
  • @phil on my phone at the moment, will write a detailed answer once I get to a computer (assuming it has not been answered). – D. Foley Jan 31 '17 at 02:36
  • Are you trying at `file:` protocol? – guest271314 Jan 31 '17 at 02:44
  • 1
    What is your HTTP server (Apache, nginx, IIS, etc)? What server-side applications / languages are available (NodeJS, PHP, python, Java, etc)? – Phil Jan 31 '17 at 02:49
  • @guest271314 1) That's unlikely given that `jQuery.get('/development/lol.txt')` presumably works. 2) Why is that relevant? – Phil Jan 31 '17 at 03:00
  • @Phil _"1) That's unlikely given that jQuery.get('/development/lol.txt') presumably works."_ The same `javascript` can return same result at `file:` protocol. _"2) Why is that relevant?"_ If `file:` protocol is used, an alternative approach would be to use ``, `html` to achieve requirement by uploading, editing, saving file to local filesystem [Edit, save, self-modifying HTML document; format generated HTML, JavaScript](http://stackoverflow.com/questions/30563157/edit-save-self-modifying-html-document-format-generated-html-javascript) – guest271314 Jan 31 '17 at 03:02
  • jQuery is client side code. You'll need to handle this on the server. What server are you running? Do you have access? – The Qodesmith Jan 31 '17 at 03:28

1 Answers1

1

Note, This is just an example of a concept, there are more details you must take into account in a production environment

There is limited information about your development environment (i.e web server software, server-side language used). But here is what I would do in the case of a LAMP (linux, apache, mysql, php) setup.

I create a javascript function to handle write to the remote text file:

function logOnLoad()
{
    var sData = "Page has been loaded";

    $.ajax({
        url:"https://example.domain.com/logData.php",
        data: {sData: sData},
        method:"post"
    }).done(function()
    {
        alert("Finished Async write to server side file.");
    })
}

I then subscribe this function to an event handle (it can be any other event handler though, it's up to you in regards to when you want to write this data), for this example I just use onload on the body tag:

<body onload="logOnLoad()">
<p>test</p>

</body>

So now my javascript function executes onload of the html page. Now we need to write the server side code to handle the ajax request, so I create a php file called logData.php and put this in the file:

<?php

function writeData($sData, $sFilePath)
{
    $sTargetFile = fopen($sFilePath, "a");

    fwrite($sTargetFile, $sData);
    fclose($sTargetFile);
}

if(isset($_POST['sData']))
{
    writeData($_POST['sData'], "testLog.txt");
    echo "done";
}

Now whenever I load test.html, it takes the static content from the javascript variable sData and does a POST against the logData.php, the logData.php file in return looks for the file specified in the writeData function call (in this example I look for testLog.txt). It creates a file handle from this information and we do a fwrite($sTargetFile, $sData) and this writes the content to the server side text file.

D. Foley
  • 1,034
  • 1
  • 9
  • 23
  • i recieve an error `POST https://trialsite10.netlify.app/logData.php 404` . wHAT SHOULD I DO. PLEASE HELP!!! – Prakhar Parikh May 31 '22 at 16:03
  • The php file is in the same directory as the htm file... The name of HTML file is : `index.html`, `script.js`, 'logData.php', `testLog.txt`... all the files are in same folder – Prakhar Parikh May 31 '22 at 16:17