This question already has answers here:
Answer
Solution:
You will have to send request to the backend for this. The javascript variable cannot be read by your PHP code. Which means there's no way to do this without making a request to the backend.
Main reason:
PHP runs before the Javascript to deliver results to the browser.
Source