Javascript runs on the client. It does not normally have the ability to write anything to a text file, and if it did it would only be on the client. PHP runs on the server, and has no direct access to any files on the client.
Without knowing why you want to do what you requested, it's difficult to suggest an alternative; but it may be that you are looking for some sort of AJAX solution where JavaScript sends data via a HTTP request to a PHP script on the server.