I have a table on my website that is populated with values for each row, and here is a sample code:
//JAVASCRIPT
<tr onclick="window.history.replaceState(null, null, 'myPage.php?ID=2');">
The URL is changing, however it does not assign $_GET variable in PHP when using 'history.replaceState' function
Here is code to check whether ID has been assigned:
//PHP
if (isset($_GET["ID"])) {
// do something
}
I can use different function which is
//JAVASCRIPT
<tr onclick="window.location.replace('myPage.php?ID=2');">
Here, the URL is changing and the variable is actually populated with 2, however the website is refreshing as well which is something I want to avoid.
I checked many articles online and apparently you can use AJAX, however I have no experience in AJAX, that's why I could use some help
You need to use ajax
try this
//html
<tr onclick="sendData('file.php','2');">
//PHP file.php
if (isset($_GET["ID"])) {
// do something
}
return true;
//JS
function sendData( file, id){
window.histor`enter code here`y.replaceState(null, null, file+'?ID='+id);
$.ajax({
url: file,
type:GET,
data:{ID:id},
success: function(result){
console.log(result)
}});
}