PHP and Javascript work at different times. PHP generates the page and from there 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 defective for your case, is to make a form and pass the information with the refresh of the page.
The alternative you are looking for here is AJAX. A connection/call next to the server where you can pass data and receive after a few milliseconds. An example would be 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 AJAX Success function on the client side.
I hope it helps to understand the mechanism.
Dude, it’s not possible to run PHP code inside Javascript code, first because it doesn’t compile, second, because javascript only runs in the browser, and PHP only on the server, for you to create communication between the browser and the server is needed first, establish a communication via Httpxmlrequest or Ajax through a
POST
– Marcelo Aymone
Since you are using jquery, use ajax to send/receive php values.
– rray
OK, I thought it might be possible. thanks anyway!
– pc_oc
I found the tutorial below, just did not test to see if it works. http://www.mauricioprogramador.com.br/posts/passar-variavel-javascript-para-php
– user25081