0

I'm working on a webapp that is currently running on a server, where there are also some .JSON files, which I would like the user to be able to type some information and press enter where it will be stored to the .JSON file.

The webapp is written in HTML, CSS, and vanilla Javascript, there aren't any external libraries being used.

I am already pulling information from the .JSON files to be used in the app using the GET function, and I know I can't directly modify server side files with javascript unless I'm running a Node.js server (which isn't currently an option).

So I believe my only option is to use a server side language such as PHP (which I nothing about), to modify the file. My question is, how can I do this relatively simply? Possibly when a JS function is run to push the change to the file.

Can anyone give me a sliver of example code, or point me in the direction of some simple documentation or tutorial on how to do this, I'm not very adept at server side programming at all, and as this is a simple project just for me, I don't want to dive deep into PHP at the moment.

Thank you in advanced!

1 Answers1

1

So I believe my only option is to use a server side language such as PHP (which I nothing about), to modify the file.

Since you already know JavaScript, I'd revisit why you feel you can't use Node.js server-side. In any case, if you're going ahead with PHP...

To write files, file_put_contents().

To encode JSON, json_encode().

Note that there are other servers out there. You don't have to write your own stuff in PHP. If you don't need any checking on what's being sent, you can probably even modify your web server's config to accept a PUT.

Brad
  • 159,648
  • 54
  • 349
  • 530
  • I'm hoping to use this project for optional class credit once I finish it, but there is a set rule that we must use a "normal server" such as apache and no frameworks are to be used, later on once I've turned it in, I plan on moving to node.js, because it's something I'd like to learn. – Christian Smith Mantas May 18 '20 at 17:10
  • 1
    @ChristianSmithMantas There's certainly nothing abnormal about Nginx... it's used more commonly than Apache these days. Why someone would build something new on Apache, I have no idea. In any case, Apache also supports WebDAV. https://httpd.apache.org/docs/2.4/mod/mod_dav.html The server-side dependencies for running PHP are far higher than running Node.js. If you wanted, you could even configure your Apache server (for whatever reason) to proxy to Node.js. This is a common configuration, and is analogous to the method Apache uses to connect to PHP anyway. – Brad May 18 '20 at 17:16
  • @ChristianSmithMantas NodeJS can be configured to run under Apache, if so desired. Just type "nodejs with apache" into google. So can other languages. – ADyson May 18 '20 at 17:20
  • @Brad I don't mean to say that Nginx is abnormal, the problem which I should of clarified is that the professor wants to be able to take our files and drop them on to his server, and have it work, we have no access to modify the server were he'll run our code. But thank you for the example, I've looked it over and it will definitely be useful in the future :) – Christian Smith Mantas May 18 '20 at 17:22