Separate your JS and PHP code. Put your PHP in a separate php file on your server, and make it return a JSON object for example.
In your Javascript, client-side this time, make use of $.get to call your php script, and process the returned JSON in your javascript code, in the $.get callback function.
The techno you will be using by this way is called AJAX https://en.wikipedia.org/wiki/Ajax_(programming).