Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

176
Visualizações
impossible to pass js variable to php code

i have this code

 function getPackage(id) {     
     console.log(id)
     }
<button onClick="getPackage('.$rRow["id"].')" type="button" data-bs-toggle="modal" data-bs-target="#exampleModalScrollable" data-bs-placement="top" onClick="myFunction();" title="Download Playlist" class="btn btn-light waves-effect waves-light btn-xs" ><i class="mdi mdi-video-switch"></i></button>

so when i clic on button i pass the value of id to a function getPackage() and i can see the value in console.log(id)

now i have a php code :

 <?php $rUser = getUser("61"); ?>

my question is how i can pass the value of js variable console.log(id) to my php code ?? i need to change the number "61" by the js variable id , for exemple like this :

<?php $rUser = getUser("id"); ?>

any one have idea ? thank you

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

By writing PHP code and JS in the same file as you did is to dynamic generate client's side HTML/JS code using PHP, the direction is [server->client], so using this method will never able to send information from client to server.

You've two solutions:

  1. Form submit [client->server] communication (involve page navigation or open new tab)
  2. AJAX (run in background, no page navigation involved)

You may try JQuery to ease up using JS For option no.2

If you're using pure JS, then use this:

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
        // Typical action to be performed when the document is ready:
        document.getElementById("demo").innerHTML = xhttp.responseText;
    }
};
xhttp.open("GET", "your link", true);
xhttp.send();

If you're going to use JQuery, then use this:

$.ajax({
  url: "test.html",
  data: "param1=value1&param2=value2",
  method: "POST" // OR GET
}).done(function() {
  // X: Code here will only run after server responded
});

// Y: Code here will run first before X
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda