PHP is server side and will be able to write to your server file
js (*) is client side and will not be able to write to your server
They work well together if you code them to work well together.
PHP has to manage the "server stuffs" (in your example, the file writing)
JS can help (but it's not mandatory) to get the client stuffs nicer or easier to use.
One thing is what the client view when it visits a php page (the HTML code is created by the php. the code you reported is NOT what a client sees)
Another thing is what the server has (what you posted is a php file)
You have to take some step backward
btw, "post and get" are the easier way to start with these sort of things.
Then you'll get to "but my page refreshes at any command" and we well able to talk about js again, but it will be in the future
(*) node js and blah blah bla.. i know, but it's not the point