If you are not doing something truly esoteric (e.g. server side JS and PHP):
PHP works, generates a response, sends it to the browser. If this answer includes JavaScript, then this JS will be executed.
You cannot get data from JS back to PHP without requesting a new HTTP request.
This can be done by setting document.location , adding an <img> element to the document with the data passed through the src attribute (in both cases, including data in the query string, placing <form> to a <iframe> using the XMLHttpRequest object and many others methods.
It really depends on what you want to achieve.
source share