for my page
www.domain.com/page.php?x=1&y=2&z=3
how can i set the $_GET
variables inside the AJAX $.get()
?
$.get('page.php',
{x: x, y: y, z: z},
function(){
})
how do i define the $_GET
using jQuery
or JavaScript
? i can have 1 2 3
and 4 5 6
and 7 8 9
you can assing the value at a var or assign directly
var x = 10;
var y = 100;
var z = 1000;
$.get('page.php',
{x: x, y: y, z: z},
function(){
})
or
$.get('page.php',
{x: 10, y: 100, z: 1000},
function(){
})
Like this (source: here and here):
var getUrlParameter = function getUrlParameter(sParam) {
var sPageURL = decodeURIComponent(window.location.search.substring(1)),
sURLVariables = sPageURL.split('&'),
sParameterName,
i;
for (i = 0; i < sURLVariables.length; i++) {
sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] === sParam) {
return sParameterName[1] === undefined ? true : sParameterName[1];
}
}
};
Usage example:
var x = getUrlParameter('x');
var y = getUrlParameter('y');
EDIT: Or if you also use PHP for this site you could just do:
var x = <?php echo json_encode($_GET['x']); ?>
As puelo already answered. you have to extract the parameters from the url
I'm just adding a reminder that you can get the url of the ajax request -not the url of the current document- like this
$.get('page.php',
{x: 1, y: 2, z: 3},
function(data){
console.log(this.url);
}
);
this will log
page.php?x=1&y=2&z=3
which you can extract the parameters from later