-2

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

Calibur Victorious
  • 638
  • 1
  • 6
  • 20

3 Answers3

0

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(){
 })
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
0

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']); ?>
puelo
  • 5,464
  • 2
  • 34
  • 62
0

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

Accountant م
  • 6,975
  • 3
  • 41
  • 61