PHP and JavaScript work at different times. PHP generates the page and from there there is only HTML and JavaScript. So it is not possible to match variables that belong to different worlds: server-side PHP and client-side JavaScript.
There are, however, two ways to communicate "between worlds." One of them, too defenitive for your case, is to make a form and pass the information with the page refresh.
The alternative you are looking for here is AJAX. A call / call on the server side where you can pass data and receive after a few milliseconds. An example would look like this:
$.ajax({
type: "POST",
url: "seuFicheiro.php",
data: {nomeVariavel: 'valor variável',
success: function (data) {
// aqui pode usar o que o PHP retorna
}
});
And on the PHP side something like:
$nome = $_POST['nomeVariavel'];
// correr outro código que precise...
echo $resposta;
This echo
is what is passed to the client-side AJAX success function.
Hope it helps you understand the mechanism.