Javascript works on the user's computer, PHP works on your server. In order to send variables from JS to PHP and vice versa, you need communication such as a page load.
Have a look here on what you can do: How to pass JavaScript variables to PHP? or more specifically the first answer (https://stackoverflow.com/a/1917626/1311593)