0

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

Rocma Brutus
  • 1
  • 1
  • 4
  • how can you pass js variable to php not mentioned in the answer – Rocma Brutus Apr 05 '22 at 22:42
  • this is not how it works, passing js to PHP in the same file is impossible as the PHP "server-side" is rendered before the code returned to the client-side "before rendering the js" what you can do is use ajax request to send request from the client-side to server-side and retrieve any data accordingly and render it – Omar Tammam Apr 06 '22 at 00:18
  • thank you omar for explication , can you help me to configure this please ? im not really good in js or ajax – Rocma Brutus Apr 06 '22 at 01:28
  • sure will try to write a simple example to illustrate the idea in UI screen : – Omar Tammam Apr 06 '22 at 02:20
  • in UI screen : `
    user id
    `
    – Omar Tammam Apr 06 '22 at 02:21
  • getuser.php as a simple example `$user = ["id" => 1 , "name" => "name test"]; echo json_encode($user); test the mentioned snippet and realize the browser network & console after adding submitting however, you need to read more about PHP with ajax please check the following link as an entry point https://www.w3schools.com/php/php_ajax_php.asp – Omar Tammam Apr 06 '22 at 02:24
  • thank you sir , so in your exemple where i pt the value of id ? i have to declare the value in button no ? this is the php variable i have to pass '.$rRow["id"].' – Rocma Brutus Apr 06 '22 at 15:24
  • mister Omar , did you have skype or whatsaap so you can show me how i do that with my code ? i can pay you if you want , thank you boss – Rocma Brutus Apr 06 '22 at 15:44
  • what i have to do is , onclic i have to get the id , after i have to put the value of the id in php variable , after i have to open modal – Rocma Brutus Apr 06 '22 at 15:47

1 Answers1

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