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
    },
    ...
2

2 Answers 2

1

Check out http://jquery-howto.blogspot.com/2009/09/get-url-parameters-values-with-jquery.html

Sign up to request clarification or add additional context in comments.

Comments

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)

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.