2

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>"; 
1
  • You can add a change event handler to the select element that calls submit() on the form element. Commented Feb 29, 2012 at 17:03

6 Answers 6

5

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(); 
    });
Sign up to request clarification or add additional context in comments.

Comments

3

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();
});

Working demo here

Comments

1
<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.

1 Comment

Usage of inline scripts is deprecated.
1

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.

Comments

0

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
  });
})

Comments

0

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.

Comments

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.