Submit a form without using submit button

forms, javascript

Solution

Make a function to check everything you want has been set, and then if it has, submit the form:

function submitIfFormComplete()
{
  // Check the select has something selected
  if (document.getElementById('selectOne').selectedIndex > 0)
  {
      document.getElementById('formID').submit();
  }
}

Then on your select, bind the onchange event to run the function.

Here is a working example: http://jsfiddle.net/JE6AM/

Select your car make:
<select id='sel1' name='selectCar' onchange="checkAndSubmit()">
    <option value="0">Select...</option>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select> 
<br/><br/>
Select your gender:
<select id='sel2' name='selectGender' onchange="checkAndSubmit()">
    <option value="0">Select...</option>
  <option value="Male">Volvo</option>
  <option value="Female">Saab</option>
</select> 

Javascript:

function checkAndSubmit()
{
  if (document.getElementById('sel1').selectedIndex > 0
     && document.getElementById('sel2').selectedIndex > 0)
  {
      //document.getElementById('formID').submit();
      alert('both have been selected!');
  }
}

I've replaced the submit with an alert() to show you how the code triggers.

Edit: You can use `$_REQUEST['selectCar']` to access the value.

Problem

I am using a form to submit the data to get the records from the database. In the form I am using the two select tag options. So after selecting the options the form should submit without using the submit button. I am waiting for the response to submit the form after selecting the inputs without using the submit button(or any button) it should submit automatically.

Original source