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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with dependent drop downs

Status
Not open for further replies.

PushCode

Programmer
Dec 17, 2003
573
US
Two tables, one is ecom_manufacturer, one is ecom_model.

ecom_manufacturer table.
mfr_id, mfr_name

ecom_model table
model_id, model_name, mfr_id

I'm able to populate the first drop down with the manufacturer names. But I'm having trouble populating the second (models) based on which manufacturer is selected in the first. I know this is a typical problem, but I haven't been able to find a solution that fits my needs.

If anyone could take a look at my code and let me know what I'm doing wrong I'd appreciate it. Here's the code
Code:
<!--#include file="includes/_variables.inc" -->
<%Response.Buffer = true%>
<HTML>
<HEAD>


<script language = "JavaScript">
function subcat()
cat = document.subad.catagory[document.subad.catagory.selectedIndex].value;
url = "submitad.asp?cat="
url = url + cat;
window.location.href< = url;
function sublist(inform, selecteditem)
inform.subcatagory.length = 0 
<% 
Set rs = Server.CreateObject("ADODB.Recordset")
rs.ActiveConnection = strServerConnection
rs.Source = "select * from ecom_model where mfr_id =" %> & cat
<% rs.Open() 
x=0

count= 0
y=0
do while not rs.eof 
%>
x = <%= trim(y) %>
subcat = new Array();
subcatagorys = "<%= trim(rs("model_name"))%>"
subcatagoryof = "<%= trim(rs("mfr_id"))%>"
subcatagoryid = "<%= trim(rs("model_id"))%>"
subcat[x,0] = subcatagorys;
subcat[x,1] = subcatagoryof;
subcat[x,2] = subcatagoryid;
if (subcat[x,1] == selecteditem)
{ 
var option<%= trim(count) %> = new Option(subcat[x,0], subcat[x,2])
inform.subcatagory.options[inform.subcatagory.length]=option <%= trim(count)%>
<%
count = count + 1
y = y + 1
rs.movenext
loop
%> 
</script>
<title>Submit an Ad</title>
<LINK rel="stylesheet" type="text/css" href="style/summary.css">
</HEAD>
<BODY onLoad = "sublist(document.subad,document.subad.catagory[document.subad.catagory.selectedIndex].value)">
<form name = subad action = "" >
<table>   
<tr>
<td>Headline</td>
</tr>
<tr>
<td><Input Name = "headline" MaxLength = "100" value="" size=50></td>
</tr>
<tr>
<td>Description</td>
</tr>
<tr>
<td><textarea name = "description" rows = 5 cols = 50></textarea></td>
</tr>
<%
cat = Request.QueryString("cat")
Set rs = Server.CreateObject("ADODB.Recordset")
rs.ActiveConnection = strServerConnection
rs.Source = "SELECT * from ecom_manufacturer"
rs.Open() 
%>
<tr>
<td>Catagory</td>
</tr>
<tr>
<td><SELECT id=catagory name=catagory onChange = "sublist(this.form, document.subad.catagory[document.subad.catagory.selectedIndex].value)">
<OPTION selected value=""></OPTION>
<%
do until rs.eof
%>
<OPTION value="<%= rs("mfr_id")%>"><% = rs("mfr_name")%></OPTION>
<%
rs.movenext
loop
set rs = nothing
%>
</SELECT></td>
</tr>
<tr>
<td><SELECT id = "subcatagory" name="subcatagory">
<Option selected value="none">-------------------------</option>
</SELECT>
</td>
</tr>
<tr>
<td>Price</td>
<tr>
<tr>
<td><INPUT type="text" id=price name=price></td>
</tr>
<td><Input type=submit value = "Submit" id=submit1 name=submit1 ></td>
</tr>
</table >
</form>
<%
'Else
'Response.Redirect url
'End if
%>
</BODY>
</HTML>

Thanks!
 
Unfortunately I see several problems.
First thing is that javascript syntax has brackets around the contents of a function:
Code:
function myFunc(a,b,c){
   // code here
}

Next problem is that ASP code executes before anything happens client-side and then the server sends the page. This means that any client-side code (like javascript) that had ASP blocks in it has already had the ASP portion run and cannot call ASP code to run again. The ASP code is only responsible for producing a file and sending it to the browser, once it finishes that task it is done.

There is a FAQ for chained dropdowns that uses a client-side array to hold the data so that no page refresh is required. It looks as if that is what your trying to do here. You may want to take a look at it as you have several places where it looks like you were expecting the ASP code to be executed after the javascript, which is backwards from how it works.

 
Thanks, I figured it out with the help of an FAQ.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top