Reputation: 221
I would like use Jquery to submit a form automatically once I select a user from the dropdown menu. I don't want any submit button to submit the form. How do I do that with Jquery?
I have the code as follows:
$table .= "<form method='post' id='change_user'><select name='hpn_user'>";
foreach($hpnusers as $hpnuser){
$table .= "<option value='". $hpnuser['id'] . "'>" . $hpnuser['first_name'] . "</option>";
}
$table .= "</select><input type='hidden' name='submit' value='1' ></input></form>";
Upvotes: 2
Views: 18879
Reputation: 28773
We Can also try like this:
$("#DropDown").change(function(){
$("#myform").attr("action", "Your URl/"+attr);
$("#myform").submit();
})
The attribute value is the optional one....unless you want to post some.
May hope it useful.
Upvotes: 1
Reputation: 21575
User jQuery's .submit()
method:
$('#change_user').submit();
If you want to submit the form once an item from the dropdown is selected, add a change
handler to the dropdown and place the call to submit()
within it:
$('select[name="hpn_user"]').on('change', function(){
$('#change_user').submit();
});
Upvotes: 3
Reputation: 3965
<form method='post' id='change_user' name="change" onChange="javascript:
document.forms['change'].submit()">
Another Example:
<form action="./" method="GET">
<div align="center"">
<select name="state" onchange="this.form.submit();">
<option>Choose One To Submit This Form</option>
<option value="CA">CA</option>
<option value="VA">VA</option>
</select>
</div>
</form>
Example using Jquery
$('hpn_user').change(function() {
document.forms["change"].submit();
});
Add Name Attribute in form
In my example i am considering name=change
in your form.
Upvotes: 1
Reputation: 150313
Bind a change event to the select
element that will submit the form on change:
$('select[name="hpn_user"]').change(function(){
$('#change_user').submit();
});
submit
docs:
Description: Bind an event handler to the "submit" JavaScript event, or trigger that event on an element.
Upvotes: 0
Reputation: 682
User jquery's .change() to recognize the value of the drop has changed and pass in the form to submit.
Ex.
$('userDropdown').change(function() {
$('form').submit();
});
Upvotes: 5
Reputation: 18257
First you can trigger the event assuming that the form it's called form1
$('form#form1').trigger('submit');
Second you could make an ajax called in the event change of your dropdownlist Assuming that you dropdownlist is called example
$('select#example').change(function(){
$.ajax({
//your options
});
})
Upvotes: 0