Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
68
rated 0 times [  75] [ 7]  / answers: 1 / hits: 20929  / 12 Years ago, mon, june 4, 2012, 12:00:00

Possible Duplicate:

how to get GET and POST variables with JQuery?






I have the following HTML:



<form action='.' method='post'>{% csrf_token %}
<div class=parameters>
Show
<select name=earnings_filter>
<option value=all>Total earnings</option>
<option value=hd>HD earnings</option>
<option value=sd>SD earnings</option>
</select>
<input type=submit name=submit class=submit float-right value=submit id=submit_financials/>
</div>
</form>


I need to do an ajax call with this, which I'm triggering on:



$(#submit_financials).live('click', function(){
...
});


Is there a way to get the variables that are submitted in POST, for example which option was selected (and there are about 10 other variables I need to get). Or do I need to use the jQuery selectors to get the value of each?


More From » jquery

 Answers
57
$(#submit_financials).live('click', function(){
$.ajax({
url: '', // script url to send
method: 'POST', // method of sending
data: $('form').has(this).serialize(), // .serialize() make query string with form inputs name and value
dataType:'json', // expected data format returned from server, you may have something else
success: function(response) {
// response contains data returned from server
}
});
});


It would be better replace live() with .on() if you're using jQuery > 1.7 and it'd be better if possible. So you can write it



$(#container).on('click', '#submit_financials', function(){
$.ajax({
url: '', // script url to send
method: 'POST', // method of sending
data: $('form').has(this).serialize(), // .serialize() make query string with form inputs name and value
dataType:'json', // expected data format returned from server, you may have something else
success: function(response) {
// response contains data returned from server
}
});
});


Here #container point to holder of #submit_financials that belong to DOM at page load.


[#85163] Saturday, June 2, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tayla

Total Points: 681
Total Questions: 102
Total Answers: 108

Location: Marshall Islands
Member since Tue, Sep 21, 2021
3 Years ago
tayla questions
Fri, Mar 5, 21, 00:00, 3 Years ago
Wed, Oct 28, 20, 00:00, 4 Years ago
Thu, Apr 9, 20, 00:00, 4 Years ago
;