2

How do I get a particular GET variable in JavaScript or jQuery?

I want to pass it on in ajax script in this sort of way:

$.ajax({
    url: 'foo/bar.php',
    data: { 
       search: $(this).val(),
       page: something //$_GET['page'] only in js
    },
    ...
bcmcfc
  • 25,966
  • 29
  • 109
  • 181
  • possible duplicate of [JavaScript query string](http://stackoverflow.com/questions/647259/javascript-query-string) – Quentin Oct 28 '10 at 13:25
  • possible duplicate of [How can I get query string values?](http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values) – Spudley Sep 06 '13 at 10:21

2 Answers2

0

what you try is almost correct, but you dont hav to label the data and you have a wron placed } in your code.

$.ajax({
    url: 'foo/bar.php',
    { 
       search: $(this).val(),
       page: 'something'
    },
    ...
});

for more information, take a look at the documentation.

EDIT: to read your get-variable, just do it like you always do: $s = $_GET['search'];. maybe you have to use $.get instead of $.ajax or set the request type for your $.ajax-call (don't know if it's POST by default, but you should be able to see this using firebug or something similar)

oezi
  • 51,017
  • 10
  • 98
  • 115
  • You can ignore that, that was basically just a type-o when I was providing an example of where I wanted to use it. The actual question is how to read the GET variable itself. – bcmcfc Oct 28 '10 at 13:24