Skip to content

Change form action based on drop down selection

Need form to change it’s action depending on the selection from a specific drop down menu. On change should trigger the script and change the action before user submits. Easier said than done when you’re new to JS.. thanks for any help!

Javascript:

<script type="application/javascript">

function chgAction(form1){

    if( recipient=="jordachedotcom_Advertising" )
    {document.form1.action = "/adv_contact.php";}

    else if( recipient=="dept_Public_Relations" )
    {document.form1.action = "/pr_contact.php";}

    else if( recipient=="dept_Manufacturing" )
    {document.form1.action = "/manuf_contact.php";}

    else if( recipient=="dept_Brands" )
    {document.form1.action = "/brands_contact.php";}

    else if( recipient=="dept_Holdings" )
    {document.form1.action = "/holdings_contact.php";}

    else if( recipient=="dept_Vendor_Inquiry" )
    {document.form1.action = "/vend_contact.php";}

    else if( recipient=="dept_Other_Inquiry" )
    {document.form1.action = "/misc_contact.php";}

    }
</script>


FORM HTML:

<form id="form1" name="form1" method="post" action="/">

Please choose a dept:<br/>
      <select name="recipient" id="recipient" size="1" onChange="javascript:chgAction()">
      <option value="" selected="selected">Select</option>
      <option value="dept_Advertising">Advertising</option>
      <option value="dept_Public_Relations">Public Relations</option>
      <option value="dept_Manufacturing">Manufacturing</option>
      <option value="dept_Brands">Brands</option>
      <option value="dept_Holdings">Holdings</option>
      <option value="dept_Vendor_Inquiry">Vendor Inquiry</option>
      <option value="dept_Other_Inquiry">Other Inquiry</option>
      </select>

<input type="submit">
</form>

Answer

Your code is missing the part to get the selected item from the selectbox.

document.form1.recipient.selectedIndex

The rest should be ok and i have created a Fiddle