I have an if-else condition in javascript. In every else condition I want to pass a parameter id with its value in url on & basis like in first else condition code passed a value 5 with id variable.
http://localhost/cah/blank.php?id=5
in second condition
http://localhost/cah/blank.php?id=5&id=9
and so on. Likewise I can have multiple conditions in which I have to pass different values of id in url. how can it be done? Either by storing them all in an array or pass values in url on & basis but without refreshing page.
var count=0;
var counts=0;
function check() {
var a = document.getElementById("div1") ;
var b = document.getElementById('div1').getAttribute('value');
if (a.textContent==b){
count++;
var p1=document.getElementById('cnt');
p1.value=count;
a.style.color = "green";
}
else {
counts++;
a.style.color = "Red";
window.history.pushState("object or string", "Title", "./blank.php?id=<? echo $id;?>");
$('#d<? echo $d3++;?>').addClass('disabled');
}
}
Use array-style names for the parameters:
http://localhost/cah/blank.php?id[]=5&id[]=9
PHP will then make $_GET['id'] return an array of all the parameters.
var ids = [5, 9];
var id_params = ids.map(function(id) {
return 'id[]=' + id;
}).join('&');
var url = 'http://localhost/cah/blank.php?' + id_params;
console.log(url);
In PHP it would be:
<?php
$ids = array(5, 9);
$id_params = implode('&', array_map(function($id) {
return 'id[]=' . $id;
}, $ids));
?>
window.history.pushState("object or string", "Title", "./blank.php?<?php echo $id_params;?>");