Solved

<option in <select statement needs to use onChange event to change action

Posted on 2011-03-08
2
290 Views
Last Modified: 2012-05-11
I have the following form where the options will in some cases dictate a change in action.  This is not working, how do I get this form to work?  I cannot put the onChange on the select statement because I need it to change based on the <option selected.

<form style="margin:0px;" action="/lee-county/residential?frame=RESI" method="post">
<table width="100%" border="0" cellspacing="0" cellpadding="3">
    <tr>
      <td><div style="padding-bottom:4px;">City: <select name="search_city">
        <option value="Alva">Alva</option>
        <option value="Bonita Springs" onChange="this.form.action='?frame=RESI&base=frames-collier';" >Bonita Springs</option>  
        <option value="Another" onChange="this.form.action='?frame=LOTL&base=frames-sanibel';" >Another</option>  
      </select></div>                   
<input type="image" id="WADbSearch1" name="WADbSearch1" src="/images/search.png" value="Submit">
		</form>                                                            
                        </td>
    </tr>
  </table>	
</form>

Open in new window

0
Comment
Question by:pda4me
2 Comments
 
LVL 15

Accepted Solution

by:
ncoo earned 500 total points
ID: 35072187
Add the following javascript to your page:

 
<script>
function dosomething() {
	var search_city = document.getElementById('search_city');
	var form = document.getElementById('form');
	if (search_city.value=='Bonita Springs') {
		form.action='?frame=RESI&base=frames-collier';
	} else if (search_city.value=='Another') {
		form.action='?frame=LOTL&base=frames-collier';
	}
	//comment out line, for testing only
	alert(form.action);
}
</script>

Open in new window


Update the html as follows:  

<form style="margin:0px;" action="/lee-county/residential?frame=RESI" method="post" id="form">
<table width="100%" border="0" cellspacing="0" cellpadding="3">
    <tr>
      <td><div style="padding-bottom:4px;">City: <select id="search_city" name="search_city" onchange="dosomething()">
        <option value="Alva">Alva</option>
        <option value="Bonita Springs">Bonita Springs</option>
        <option value="Another">Another</option>
      </select></div>
<input type="image" id="WADbSearch1" name="WADbSearch1" src="/images/search.png" value="Submit">
		</form>
                        </td>
    </tr>
  </table>
</form>

Open in new window


The onchange is on the select but we're checking the value each time.

Comment out the alert, it's only there for testing.
0
 

Author Closing Comment

by:pda4me
ID: 35072306
U R A ROCKSTAR!!!!!  THANKS SO MUCH!!!
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question