Skip to content
Advertisement

How do you modify a file using JavaScript together with PHP?

I want to add text to a text document using JavaScript and PHP. What would be the best way to do this?

Advertisement

Answer

This is possible by using Javascript (front-end) to send an request to the PHP server script that does the operation (back-end).

What you can do is use jQuery.ajax or XMLHttpRequest.

XMLHttpRequest

JavaScript

jQuery.ajax

JavaScript

Note: There is also the jQuery.post method, but I have not included it.

And, in the PHP file, with the necessary permissions, you can write to the file using fwrite in combination with the other file functions.

JavaScript

If you want to open the file in a different mode, there is a list of modes on the PHP website.

All the filesystem functions can be found here on the PHP website.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement