0

I have a question. I have a page that generates strings from 4 to 8 characters, and I want them to be stored in a textfile in the page's folder.

Is there anything on Javascript or jQuery that would allow me to do this? I would prefer not to use PHP, as I don't know anything about it, but if I must, I will.

RobSp1derp1g
  • 63
  • 1
  • 9
  • Is the file you want to write to on the server or client side? That will make a big difference in the answers. – CDspace Feb 02 '17 at 15:54
  • The text file will be on the server. – RobSp1derp1g Feb 02 '17 at 15:57
  • What is "the page's folder"? – guest271314 Feb 02 '17 at 15:58
  • "The page's folder" is the folder in the server where the page is located. – RobSp1derp1g Feb 02 '17 at 16:00
  • `javascript` cannot write to server alone, without server interaction; save for use of `nodejs` server side. You can write to user filesystem at chrome, chromium; or offer file to user for download. – guest271314 Feb 02 '17 at 16:02
  • I would save them into the `window.localStorage` Check [this](http://www.w3schools.com/html/html5_webstorage.asp) for more information – Nicolas Feb 02 '17 at 16:04
  • @Nicolas, using LocalStorage would not work, because it doesn't access the file... – RobSp1derp1g Feb 02 '17 at 16:17
  • @RobSp1derp1g Do you want them to be store client or server side ? – Nicolas Feb 02 '17 at 16:22
  • @Nicolas On the server. There will be two files: Google.txt and Bitly.txt, and I want the links (strings) to be saved inside the two files, which will be on the server. – RobSp1derp1g Feb 02 '17 at 16:26
  • @RobSp1derp1g Then youj need a server side language. Like PHP. As octacian stated, it is possible to have serverside javascript. I suggest [this](https://www.tutorialspoint.com/nodejs/nodejs_express_framework.htm) link for information on the express framework and [this](http://stackoverflow.com/questions/17981677/using-post-data-to-write-to-local-file-with-node-js-and-express) one for saving files on the server. – Nicolas Feb 02 '17 at 16:58
  • Keep an open mind about server-side technologies. There are dozens that would work for you, you aren't automatically bound to PHP. – Cᴏʀʏ Feb 02 '17 at 18:34

1 Answers1

3

The basic answer, is no. JavaScript (and therefore JQuery) run on the client browser. This means, that they cannot access files on the server. Not only that, but they are not allowed to access files on the client computer to improve security. If JavaScript was allowed to, a single website could literally destroy you're computer by deleting files. Or, it could read files and steal personal information.

Your best option is to use PHP. You could also use NodeJS as your backend with the Express Web Framework. NodeJS allows you to run JavaScript on the server to both serve pages and interact with files and/or databases.

If you'd like to store data on the client [browser], use cookies, or see the more intuitive Web Storage API.

octacian
  • 128
  • 1
  • 8
  • But then how would I do it? – RobSp1derp1g Feb 02 '17 at 16:07
  • Which way would you like to do it? With PHP or NodeJS? – octacian Feb 02 '17 at 16:10
  • I should explain what I want. This is the site I'm making: http://link-randomizer.eu.pn/ I want it to copy the addresses it creates into a text file. I'd try my best to avoid PHP. – RobSp1derp1g Feb 02 '17 at 16:12
  • 1
    Install NodeJS on your server, then see [installing Express](http://expressjs.com/en/starter/installing.html). Once express is installed, see [Basic Routing](http://expressjs.com/en/starter/basic-routing.html) and [Static Files](http://expressjs.com/en/starter/static-files.html). Essentially, you need to transfer your website to NodeJS using the express framework. Then, use JQuery to send a post request to the server when a new URL is generated, handle it using the information from basic routing, and see the [filesystem](https://nodejs.org/api/fs.html) API for saving URLs to the file. – octacian Feb 02 '17 at 16:53
  • **Note:** If you are not hosting this yourself, you may have to check with your hosting provider and maybe even switch, as many do not support NodeJS. If this is the case, you may have to fall back to PHP. – octacian Feb 02 '17 at 16:54
  • Yeah, my server doesn't seem to know what NodeJS is, and it does support PHP. I just didn't know it could. – RobSp1derp1g Feb 02 '17 at 17:04
  • 1
    In that case, see the documentation for PHP [`$_POST`](http://php.net/manual/en/reserved.variables.post.php) and write a simple PHP script to take the data in `$_POST` and use [`file_put_contents`](http://php.net/manual/en/function.file-put-contents.php) to write to the file. You can query the PHP script with JQuery's `$.post`. – octacian Feb 02 '17 at 17:35