Change form action based on 2 select option

The tutorial explains on how to change form action based on the 2 select option. The use of this code is for loading different pages using the submit function of the javascript (this.form.submit();) when one of the select option changes its value ( onchange=”changeAction(this.form,this.name);”). In the example below, the first select named range will load to shop_by_price.php page and the second select named keyword will load to advanced_search_result.php page when the visitor chose from their option.

See the code in action: Demo

Here is the code:

<script type=”text/javascript”> /** * * @author Arni Cinco */
function changeAction(aForm,aValue)
{ if(aValue==”range”){
aForm.setAttribute(”action”,”shop_by_price.php”); /** load the data range in advanced_search_result.php (Example:shop_by_price.php”?range=0) */

}else{
aForm.setAttribute(”action”,”advanced_search_result.php”); /** load the data keyword in advanced_search_result.php (Example:advanced_search_result.php?keyword=steel )*/
}

aForm.submit();
}

</script>

<form class=”formsearch” action=”shop_by_price.php” method=”get”>

<p>Search Caskets by:</p>
<ul>
<li>Price:
<select name=”range” onChange=”changeAction(this.form,this.name);”>

<option selected=”selected” disabled=”disabled”>Select One</option>

<option value=”0″>Under $1,000.00</option><option value=”1″>From&nbsp;$1,000.00&nbsp;to&nbsp;$2,000.00</option>
<option value=”2″>From&nbsp;$2,000.00&nbsp;to&nbsp;$3,000.00</option>
<option value=”3″>From&nbsp;$3,000.00&nbsp;to&nbsp;$4,000.00</option>
<option value=”4″>From&nbsp;$4,000.00&nbsp;to&nbsp;$5,000.00</option>
<option value=”5″>$5,000.00&nbsp;and above</option>
</select>
</li>
<li> Type:
<select name=”keyword” onChange=”changeAction(this.form,this.name);”>

<option selected=”selected” disabled=”disabled”>Select One</option>

<option value=”hardwood”>Hardwood</option>
<option value=”wood”>Wood</option>
<option value=”steel”>Steel</option>
<option value=”brass”>Brass</option>
<option value=”copper”>Copper</option>
<option value=”marble”>Marble</option>
<option value=”bronze”>Bronze</option>
<option value=”stainless steel”>Stainless Steel</option>
<option value=”cloisonne”>Cloisonne</option>
</select>
</li>
</ul>
</form>

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • Furl
  • Reddit
  • Smarking
  • StumbleUpon
  • Technorati
  • TwitThis
  • YahooMyWeb
  • Simpy
  • Propeller
  • Fark
  • LinkedIn
  • Live
  • Mixx

2 Comments »

  1. [...] Change form action based on 2 select option | Web Developers Help b…/b [...]

    Pingback by Change form action based on 2 select option | Web Developers Help b…/b | sellconcrete.com — February 9, 2009 @ 12:31 pm

  2. [...] The tutorial explained on how to change form action base on the 2 select option. The use of this code is for loading different pages using the submit function.Page 2 [...]

    Pingback by Popular People » Blog Archive » Change Form Action Based on 2 Select Option | Web Developers Help … — February 10, 2009 @ 5:27 pm

RSS feed for comments on this post. TrackBack URL

Leave a comment