Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dropdown populated by previous dropdown selection

Status
Not open for further replies.

palmettoman

Programmer
Oct 22, 2001
5
US
I want to populate a second dropdown based on what the user selects in the first dropdown. I am populating both from the database. I know how to make the dropdowns, but I need a little help with refreshing the page or running a javascript function that will fill the dropdown.
 
You want to create an asp page that contains a form. The form will post to itself. IE. if the page you are working on is named page.asp, then the action of the form will also be page.asp. You will then want to use an if condition in your asp to see if the form has been submited or not. If the form has been submited, then you know that the user has selected an option from the first drop down. You will then request the choice from the first drop down and use that variable in generating your second sql statement.

----------
EXAMPLE
----------

<form name=&quot;form1&quot; action=&quot;page.asp&quot; method=&quot;post&quot; >
<select name=&quot;dropdown1&quot; onChange=&quot;javascript:document.form1.submit()&quot;>
<option>choice1</option>
<option>choice2</option>
</select>

</form>

<%
'CHECK TO SEE IF FORM IS POSTED
IF (request.form(&quot;dropdown1&quot;) <> THEN

'GET THE CHOICE FROM STEP 1
Dim choice1
choice1 = request.form(&quot;dropdown1&quot;)

'include sql code for the second drop down...
sql = &quot;SELECT * FROM table_name &quot;
sql = sql + &quot;WHERE columnname = '&quot;&choice1&&quot;'&quot;

'execute sql
......
%>
<!-- POPULATE DROP DOWN 2 -->
<select name=&quot;dropdown2&quot;>
<option>choice1</option>
<option>choice2</option>
</select>
<%
END IF
%>

If you have any questions, just post back!!

 
Correction....

IF (request.form(&quot;dropdown1&quot;) <> THEN

should be

IF (request.form(&quot;dropdown1&quot;) <> &quot;&quot; THEN

 
So are you saying that I would need to have the first dropdown in a form that will have an action of that same page and then a second form that will have an action of another page?
 
Yes and Maybe.

To get this page to work as you described... , then the first dropdown needs to be in a form that will have an action of that same page.

Questions??
------------
What are you planning do do with the second drop down???? Is there a dynamic step3, step4, etc. to this set of pages? Are you going to display a result set or confirmation message of some kind? Where do you want the result set/messsage to be displayed, on the same page or a seperate page?
 
I am working on a contact management database that when you select a company name in the first dropdown it will populate the location dropdown with only those that are associated with that company. The rest of the form is gathering information for an individual that will be saved to the database.

The way I see this working is to have the first dropdown in a form that will use the javascript to automatically submit the form and populate the second form so that I can contiue to enter info on the individual and when the submit button is clicked it will go to another page that will process the form.
 
Two methods of approach:

1) Two forms! Use the example code above. Add a second form around the second drop down and the other form elements of the company's info. Set the second form's action to the nextpage. You may also need to add a hidden field in the second form to carry over the company's name. As the company's name is in the first drop down and the first form. This piece of data will not be carried over unless you place it in the second form.

2) One form! Use the example code above. Control the form's action property with the use of a variable. Set the location variable depending on if the user has selected a company or not. Notice that the if condition is the same one used to display the second drop down.


<% Dim location
IF (request.form(&quot;dropdown1&quot;) <> &quot;&quot; THEN
location = &quot;page2.asp&quot;
else
location = &quot;page.asp&quot;
end if
%>
<form name=&quot;form1&quot; action=&quot;<%=location%>&quot; method=&quot;post&quot; >
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top