1

Is it possible to save the content of a variable to a file with JQuery.

I have a html form (letter) which is saved and edited later on again by the user.
I capture the whole html document in a variable, but now I want to write that variable to a file and use a cron to save it back to the DB.

here is my code thus far

$( document ).ready(function() {
    var isDirty = false;
    $("textarea").on("change", function() {
        isDirty = (this.defaultValue !== this.value);
        if (isDirty)
            this.defaultValue = this.value;
    });

    $("input").on("change", function() {
        isDirty = (this.defaultValue !== this.value);
        if (isDirty)
            this.defaultValue = this.value;
    });
});

function getPageHTML() {
    var vDocText = "<html>" + $("html").html() + "</html>";
    console.log(vDocText);
}

So I want to end up saving the content of vDocText to a file and send the file back to 4D (my database) with a php cron process.

Tim Penner
  • 3,551
  • 21
  • 36
morne
  • 4,035
  • 9
  • 50
  • 96

1 Answers1

3

jQuery is a JavaScript library and JavaScript is a client-side language, so you cannot save any data to files with it. Either you pass your data to a PHP script that stores the content on the server (e.g. in a database) or you save the data on the client (e.g. in a Cookie or using the Web Storage API).

So, to answer your question: No, it is not possible to save the content of a variable to a file with jQuery.

However, there are certain plugins that provide access to the local file system through jQuery, but I would not recommend using them. By design, JavaScript does not have access to the local file system (for security reasons). The aforementioned plugin uses a couple of hacks and workarounds to access the file system (ActiveX and Java applets).

The best alternative, in my opinion, would be to issue an AJAX request with jQuery and have a PHP script on the server take care of writing (and further processing) the file.

user1438038
  • 5,821
  • 6
  • 60
  • 94