Click to See Complete Forum and Search --> : Drop down list- javascript and asp


luv2smile
07-21-2003, 11:41 AM
Here is my problem: I have 2 drop down lists that use asp to pull in the data and javascript to make the 2nd one dependent on the first drop down list. Ex. The first is a list of manufacturers and then the list of models depends on which manufacturer the user selects.

Ok, that works fine on the search page....the user chooses to search by model/man.

But then on a seperate page, I want to bring in the values the user selected on the search page and re-create the drop down lists so the user can change their selections, but so that was they selected is highlighted when the page is loaded.


The variables are being passed to the seperate page because I can make the selected option the one the user previously selected....but when I do that, the rest of the box doesn't work correctly.

I can bring in the Manufacturer, but the model list appears blank until the user changes the manufacturer...then it works fine.


Do I need 2 functions....one to fill the drop down box and one to change it?

Help......this is my first experience at really using javascript.



Here is the search page where the user makes his selections:


<!-- #include virtual="/crtest/authenticate.asp" -->

<HTML>
<!-- #include virtual="/crtest/Header.htm" -->
<HEAD>

<%

'crtech-inv
set conn = Server.CreateObject("ADODB.Connection")
conn.open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=f:\web\database\crtest\equip.mdb"

Set comInv = Server.CreateObject("ADODB.Command")' Open the connection here
comInv.ActiveConnection = conn
comInv.CommandType = 1 ' SQL Statement, not a table or canned routine
%>

<CENTER><H1>CSS Equipment Search</H1></CENTER>

<%


' Query to get a list of valid Manufacturer Values

comInv.CommandText = "SELECT DISTINCT Equipment.Manufacturer AS Manufacturer FROM Equipment"
Set ManList = comInv.Execute

' Query to get a list of valid Manufacturer/ModelNumber combinations

comInv.CommandText = "SELECT DISTINCT Equipment.Manufacturer AS Manufacturer, Equipment.ModelNumber AS ModelNumber FROM Equipment"
Set ManModList = comInv.Execute


' Query to get a list of other valid Department Values

comInv.CommandText = "SELECT DISTINCT DeptCode FROM Departments ORDER BY DeptCode"
Set DeptList = comInv.Execute

comInv.CommandText = "SELECT DISTINCT LocalName FROM Equipment ORDER BY LocalName"
Set LocalList = comInv.Execute

comInv.CommandText = "SELECT DISTINCT UVAAssetNumber FROM Equipment ORDER BY UVAAssetNumber"
Set AssetList = comInv.Execute

comInv.CommandText = "SELECT DISTINCT UserOwner FROM Equipment ORDER BY UserOwner"
Set UserList = comInv.Execute

comInv.CommandText = "SELECT DISTINCT Location FROM Equipment ORDER BY Location"
Set LocationList = comInv.Execute

comInv.CommandText = "SELECT DISTINCT Equipment.Manufacturer FROM Equipment"
Set ManuList = comInv.Execute

comInv.CommandText = "SELECT DISTINCT Equipment.ModelNumber FROM Equipment"
Set ModelList = comInv.Execute

comInv.CommandText = "SELECT DISTINCT ID,Description FROM EquipTypes"
Set EqTypeList = comInv.Execute

comInv.CommandText = "SELECT DISTINCT Equipment.Location FROM Equipment"
Set LocList = comInv.Execute

%>



<script language="Javascript">

var ManModList = new Array();

<%
Index = 0
Do While NOT ManModList.EOF
Manufacturer = ManModList("Manufacturer")
If Manufacturer <> "" Then
ModelNumber = ManModList("ModelNumber")
If ModelNumber <> "" Then
Response.Write " ManModList[" & Index & "] ='" & Manufacturer & "|" & ModelNumber & "'" & vbCrLf
Index = Index + 1
End If
End If
ManModList.MoveNext
Loop
%>

function PopulateData( select, name, array ) {

string = "";

// 0 - will display the new options only
// 1 - will display the first existing option plus the new options

count = 0;

// Clear the old list (above element 0)

select.options.length = count;

// Place all matching categories into Options.

for( i = 0; i < array.length; i++ ) {
string = array[i].split( "|" );
if( string[0] == name ) {
select.options[count++] = new Option( string[1] );
}
}
}

</script>


<TITLE>crtech Equipment Search</TITLE>
</HEAD>
<BODY BGCOLOR="#ffffff"><font size="-2" face="Verdana, Arial, Helvetica, sans-serif"></font>



<FORM ACTION="./eqresult.asp" METHOD="POST">

<TABLE>



<TR><TD ALIGN="right">Asset Tag (Partial match)</TD>
<TD><INPUT TYPE="text" NAME="Equipment__UVAAssetNumber" SIZE="30" MAXLENGTH="30">
</TD></TR>


<TR><TD ALIGN="right">Department</TD>
<TD><SELECT NAME="Equipment__Department">
<OPTION selected VALUE="Null">(All)
<%
Do While NOT DeptList.EOF
DeptCode = DeptList("DeptCode")
Response.Write "<OPTION VALUE='" & DeptCode & "'>" & DeptCode & vbCrLf
DeptList.MoveNext
Loop
%>

</SELECT>
</TD></TR>

<TR>

<td ALIGN="right">Manufacturer</td>
<td>
<select name='Manufacturer' style="width:120;" onChange='PopulateData( Model, Manufacturer.options[selectedIndex].text, ManModList )'>
<option selected></option>
<%
Do While NOT ManList.EOF
Manufacturer = ManList("Manufacturer")
If Manufacturer <> "" Then
Response.Write " <OPTION>" & Manufacturer & "</OPTION>" & vbCrLf
End If
ManList.MoveNext
Loop
%>
</select>
</td>
</tr>
<TR>
<td ALIGN="right">Model</td>
<td><select name="Model" style="width:120;">
<option selected></option>
</select>
</td>

</TR>

<TR><TD ALIGN="right">Equipment Type</TD>
<TD><SELECT NAME="Equipment__EquipType">
<OPTION selected VALUE="Null">(All)
<%
Do While NOT EqTypeList.EOF
ID = EqTypeList("ID")
Description = EqTypeList("Description")
Response.Write "<OPTION VALUE='" & ID & "'>" & Description & vbCrLf
EqTypeList.MoveNext
Loop
%>

</SELECT>
</TD></TR>

<TR><TD ALIGN="right">Location</TD>
<TD>
<select name="Equipment__Location">
<option selected value="Null">(All)
<%
Do While NOT LocList.EOF
Location = LocList("Location")
Response.Write "<OPTION VALUE='" & Location & "'>" & Location & vbCrLf
LocList.MoveNext
Loop
%>

</select>
</TD>
</TR>


<TR><TD ALIGN="right">Serial Number (Partial match)</TD>
<TD><INPUT TYPE="text" NAME="Equipment__SerialNumber" SIZE="30" MAXLENGTH="40">
</TD></TR>

<TR><TD ALIGN="right">User/Owner (Partial match)</TD>
<TD><INPUT TYPE="text" NAME="Equipment__UserOwner" SIZE="30" MAXLENGTH="40">
</TD></TR>

<TR><TD ALIGN="right">Local Name (Partial match)</TD>
<TD><INPUT TYPE="text" NAME="Equipment__LocalName" SIZE="30" MAXLENGTH="40">
</TD></TR>

<TR><TD ALIGN="right">IP Address (Partial match)</TD>
<TD><INPUT TYPE="text" NAME="Equipment__IPAddress" SIZE="30" MAXLENGTH="40">
</TD></TR>

<TR><TD ALIGN="right">Comments (Partial match)</TD>
<TD><INPUT TYPE="text" NAME="Equipment__Comments" SIZE="50" MAXLENGTH="255">
</TD></TR>

<TR><TD ALIGN="right">Building Name (Partial match)</TD>
<TD><INPUT TYPE="text" NAME="Equipment__Building" SIZE="30" MAXLENGTH="40">
</TD></TR>

<TR><TD ALIGN="right" WIDTH="50%"><INPUT NAME="Submit" TYPE="submit" Value="Search All"> </TD>
<TD ALIGN="left" WIDTH="50%"><INPUT NAME="Submit" TYPE="submit" VALUE="Search Maintenance Logs"></TD>
</TR><TR>
<TD ALIGN="right" WIDTH="50%"><INPUT NAME="Submit" TYPE="submit" VALUE="Reset"></TD>
<TD ALIGN="left" WIDTH="50%"><INPUT NAME="Submit" TYPE="submit" Value="Add"></TD>
</TR>
</FORM>

<TR><FORM ACTION = "eqresult.asp" METHOD = "post">
<TD ALIGN="right" WIDTH="50%"><INPUT NAME="Submit" TYPE="submit" Value="SearchPC">
<INPUT TYPE="hidden" NAME="Equipment_EquipType" Value = "2">
<INPUT TYPE="hidden" NAME="Equipment_Department" Value = "css-cr">
</TD></FORM>

<FORM ACTION = "eqresult.asp" METHOD = "post">
<TD ALIGN="left" WIDTH="50%"><INPUT NAME="Submit" TYPE="submit" Value="SearchMAC">
<INPUT TYPE="hidden" NAME="Equipment_EquipType" Value = "6">
<INPUT TYPE="hidden" NAME="Equipment_Department" Value = "css-cr">
</TD></FORM>
</TR>
</FORM>
</TABLE>
</BODY>
<!-- #include virtual="/crtest/Footer.htm" -->
</HTML>

luv2smile
07-21-2003, 11:46 AM
<%

set conn = Server.CreateObject("ADODB.Connection")
conn.open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=f:\web\database\crtest\equip.mdb"

Set comInv = Server.CreateObject("ADODB.Command") ' Open the connection here
comInv.ActiveConnection = conn
comInv.CommandType = 1 ' SQL Statements, not tables or canned routines


' Query to get a list of valid Manufacturer Values

comInv.CommandText = "SELECT DISTINCT Equipment.Manufacturer AS Manufacturer FROM Equipment"
Set ManList = comInv.Execute

' Query to get a list of valid Manufacturer/ModelNumber combinations

comInv.CommandText = "SELECT DISTINCT Equipment.Manufacturer AS Manufacturer, Equipment.ModelNumber AS ModelNumber FROM Equipment"
Set ManModList = comInv.Execute

%>

<HTML>
<HEAD>
<script language="Javascript">

var ManModList = new Array();

<%
Index = 0
Do While NOT ManModList.EOF
Manufacturer = ManModList("Manufacturer")
If Manufacturer <> "" Then
ModelNumber = ManModList("ModelNumber")
If ModelNumber <> "" Then
Response.Write " ManModList[" & Index & "] ='" & Manufacturer & "|" & ModelNumber & "'" & vbCrLf
Index = Index + 1
End If
End If
ManModList.MoveNext
Loop
%>

function PopulateData( select, name, array ) {

string = "";

// 0 - will display the new options only
// 1 - will display the first existing option plus the new options

count = 0;

// Clear the old list (above element 0)

select.options.length = count;

// Place all matching categories into Options.

for( i = 0; i < array.length; i++ ) {
string = array[i].split( "|" );
if( string[0] == name ) {
select.options[count++] = new Option( string[1] );
}
}
}

</script>
<TITLE>CSS Equipment Search</TITLE></HEAD>
<BODY onLoad='PopulateData( Model, Manufacturer.options[selectedIndex].text, ManModList )'>

<CENTER>
<IMG SRC="gif/detail.gif">
</CENTER>
<CENTER><H1>CSS Equipment Data</H1></CENTER>

<%


' Run the query using the Unique Identifier passed in the URL.
comInv.CommandText = "SELECT * FROM Equipment WHERE Equipment.SerialNumber = '" & Request.QueryString("Equipment__SerialNumber") & "'"
Set GetDetailData = comInv.Execute
Do While Not GetDetailData.EOF
%>

<FORM ACTION="equpdate.asp" METHOD="POST">
<INPUT TYPE="hidden" NAME="Equipment__KeySerialNumber" VALUE="<%=GetDetailData("SerialNumber")%>">

<TABLE>
<TR>
<TD ALIGN="Right"><B>Primary User:</B></TD>
<TD><INPUT TYPE="text" NAME="Equipment__UserOwner" SIZE="25" MAXLENGTH="25" VALUE="<%=GetDetailData("UserOwner")%>"></TD>

<TD ALIGN="Right"><B>Property Contact:</B></TD>
<TD><INPUT TYPE="text" NAME="Equipment__ContactPerson" SIZE="25" MAXLENGTH="25" VALUE="<%=GetDetailData("ContactPerson")%>"></TD>
</TR>
<TR>
<TD ALIGN="Right"><B>Location:</B></TD>
<TD><SELECT NAME="Equipment__Location">
<OPTION>
<%
' Location needs to have a selection list
comInv.CommandText = "SELECT DISTINCT Facilities.Location FROM Facilities ORDER BY Location"
' comInv.CommandText = "SELECT Facilities.Location FROM Facilities WHERE Facilities.Location Like "itc*" ORDER BY Location"
Set LocationList = comInv.Execute
Do While Not LocationList.EOF
If LocationList("Location") = GetDetailData("Location") Then
%>
<OPTION selected VALUE="<%=LocationList("Location")%>"><%=LocationList("Location")%>
<%
Else
%>
<OPTION VALUE="<%=LocationList("Location")%>"><%=LocationList("Location")%>
<%
End If
LocationList.MoveNext
Loop ' LocationList
%>
</SELECT>
<TD ALIGN="Right"><B>Department:</B></TD>
<TD><SELECT NAME="Equipment__Department">
<OPTION>
<%
' Department needs to have a selection list
comInv.CommandText = "SELECT DISTINCT Departments.DeptCode FROM Departments ORDER BY DeptCode"
' comInv.CommandText = "SELECT Departments.DeptCode FROM Departments WHERE Departments.DeptName Like "itc*" ORDER BY DeptCode"
Set DeptList = comInv.Execute
Do While Not DeptList.EOF
If DeptList("DeptCode") = GetDetailData("Department") Then
%>
<OPTION selected VALUE="<%=DeptList("DeptCode")%>"><%=DeptList("DeptCode")%>
<%
Else
%>
<OPTION VALUE="<%=DeptList("DeptCode")%>"><%=DeptList("DeptCode")%>
<%
End If
DeptList.MoveNext
Loop ' DeptList
%>
</SELECT>
</TR>
<TR>
<TD ALIGN="Right"><B>Computer Name:</B></TD>
<TD><INPUT TYPE="text" NAME="Equipment__LocalName" SIZE="25" MAXLENGTH="25" VALUE="<%=GetDetailData("LocalName")%>"></TD>

<TD ALIGN="Right"><B>Comments:</B></TD>
<TD><INPUT TYPE="text" NAME="Equipment__Comments" SIZE="50" MAXLENGTH="255" VALUE="<%=GetDetailData("Comments")%>"></TD>
</TR>

<TR>
<TD ALIGN="Right"><B>IP Address:</B></TD>
<TD><INPUT TYPE="text" NAME="Equipment__IPAddress" SIZE="25" MAXLENGTH="25" VALUE="<%=GetDetailData("IPAddress")%>"></TD>
</TR>
</TABLE>
<HR>
<TABLE>

<TR>
<TD ALIGN="Right"><B>UVA Asset#:</B></TD>
<TD ALIGN="Left"><INPUT TYPE="text" NAME="Equipment__UVAAssetNumber" SIZE="16" MAXLENGTH="16" VALUE="<%=GetDetailData("UVAAssetNumber")%>"></TD>

<TD ALIGN="Right"><B>UVA Account:</B></TD>
<TD ALIGN="Left"><INPUT TYPE="text" NAME="Equipment__UVAAccountNumber" SIZE="26" MAXLENGTH="26" VALUE="<%=GetDetailData("UVAAccountNumber")%>"></TD>

<TD ALIGN="Right"><B>PO Number:</B></TD>
<TD ALIGN="Left"><INPUT TYPE="text" NAME="Equipment__PONumber" SIZE="15" MAXLENGTH="15" VALUE="<%=GetDetailData("PONumber")%>"></TD>

</TR>

<TR>
<TD ALIGN="Right"><B>Serial #:</B></TD>
<TD ALIGN="Left"><INPUT TYPE="text" NAME="Equipment__SerialNumber" SIZE="16" MAXLENGTH="30" VALUE="<%=GetDetailData("SerialNumber")%>"></TD>

<!--- Purchase Date is a text field --->
<TD ALIGN="Right"><B>Purchase Date:</B></TD>
<TD ALIGN="Left"><INPUT TYPE="text" NAME="Equipment__PurchaseDate" SIZE="12" MAXLENGTH="12" VALUE="<%=Left(GetDetailData("PurchaseDate"),10)%>"></TD>

<!--- Purchase Price is a currency field --->
<TD ALIGN="Right"><B>Price:</B></TD>
<TD ALIGN="Left"><INPUT TYPE="text" NAME="Equipment__PurchasePrice" SIZE="15" MAXLENGTH="15"
<%
If GetDetailData("PurchasePrice") <> "" Then
Response.Write " VALUE='" & FormatCurrency(GetDetailData("PurchasePrice")) & "'>"
Else
Response.Write ">"
End If
%>
</TD>
</TR>

<TR>
<TD ALIGN="Right"><B>Monitor Asset#:</B></TD>
<TD ALIGN="Left"><INPUT TYPE="text" NAME="Equipment__MonitorAsset" SIZE="16" MAXLENGTH="16" VALUE="<%=GetDetailData("MonitorAsset")%>"></TD>

<TD ALIGN="Right"><B>Monitor Serial Number:</B></TD>
<TD ALIGN="Left"><INPUT TYPE="text" NAME="Equipment__MonitorSN" SIZE="30" MAXLENGTH="30" VALUE="<%=GetDetailData("MonitorSN")%>"></TD>
<TD ALIGN="Right"><B>Vendor:</B></TD>
<TD ALIGN="Left"><INPUT TYPE="text" NAME="Equipment__Vendor" SIZE="15" MAXLENGTH="20" VALUE=<%=GetDetailData("Vendor")%>></TD>

</TR>
</TABLE>
<HR>
<TABLE>

<TR>


<TD ALIGN="Right"><B>Manufacturer:</B></TD>
<TD ALIGN="Left"><select name='Manufacturer' style="width:120;" onChange='PopulateData( Model, Manufacturer.options[selectedIndex].text, ManModList )'>
<OPTION VALUE="(New)">(New)
<%
Do While Not ManList.EOF
IF ManList("Manufacturer") = GetDetailData("Manufacturer") Then
%>
<OPTION selected VALUE="<%=ManList("Manufacturer")%>"><%=ManList("Manufacturer")%>
<%
ELSE
Manufacturer = ManList("Manufacturer")
Response.Write " <OPTION>" & Manufacturer & "</OPTION>" & vbCrLf

END IF
ManList.MoveNext
Loop
%>
</SELECT>
</TD>



<TD ALIGN="right"><B>New Mfr:</B></TD>
<TD ALIGN="left"><INPUT TYPE="text" NAME="Equipment__NewMfr" SIZE="20" MAXLENGTH="30">
</TD>
</TR>
<TR>

<td ALIGN="right">Model</td>
<td><select name="Model" style="width:120;">
<OPTION VALUE="(New)">(New)
<%
Do While Not ManModList.EOF
IF ManModList("ModelNumber") = GetDetailData("ModelNumber") Then
%>

<OPTION selected VALUE="<%=ManModList("ModelNumber")%>"><%=ManModList("ModelNumber")%>
<%
ELSE
%>
<OPTION VALUE="<%=ManModList("ModelNumber")%>"><%=ManModList("ModelNumber")%>
<%
End If
ManModList.MoveNext
Loop ' ManModList
%>
</SELECT>
</td>
<TD ALIGN="right"><B>New Model:</B></TD>
<TD ALIGN="left"><INPUT TYPE="text" NAME="Equipment__NewModel" SIZE="20" MAXLENGTH="30">
</TD>

</TR>



Code continued in next post....

luv2smile
07-21-2003, 11:48 AM
<TR>

<TD ALIGN="Right"><B>Type:</B></TD>
<TD ALIGN="Left"><SELECT NAME="Equipment__EquipType">
<%
' Equipment Type must come from existing list for this app
comInv.CommandText = "SELECT DISTINCT ID,Description from EquipTypes ORDER BY Description"
Set TypeList = comInv.Execute
Do While Not TypeList.EOF
IF TypeList("ID") = GetDetailData("EquipType") Then
%>
<OPTION selected VALUE="<%=TypeList("ID")%>"><%=TypeList("Description")%>
<%
ELSE
%>
<OPTION VALUE="<%=TypeList("ID")%>"><%=TypeList("Description")%>
<%
End If
TypeList.MoveNext
Loop ' TypeList
%>
</SELECT>
<TD ALIGN="Right"><B>Description:</B></TD>
<TD><INPUT TYPE="text" NAME="Equipment__Description" SIZE="30" MAXLENGTH="50" VALUE="<%=GetDetailData("Description")%>"></TD>
</TR>
<TR>
<TD ALIGN="Right"><B>Status:</B></TD>
<TD ALIGN="Left"><SELECT NAME = "Equipment__StatusID">
<%
' Equipment Status must come from the existing list as well
comInv.CommandText = "SELECT DISTINCT ID,Status from Status ORDER BY ID"
Set StatusList = comInv.Execute
Do While Not StatusList.EOF
IF StatusList("ID") = GetDetailData("StatusID") Then
%>
<OPTION selected VALUE="<%=StatusList("ID")%>"><%=StatusList("Status")%>
<%
ELSE
%>
<OPTION VALUE="<%=StatusList("ID")%>"><%=StatusList("Status")%>
<%
End If
StatusList.MoveNext
Loop ' StatusList
%>
</SELECT>
<TD ALIGN="Right"><B>Transfer to Dept:</B></TD>
<TD ALIGN="Left"><SELECT NAME="Equipment__TransferDept">
<OPTION>
<%
comInv.CommandText = "SELECT DISTINCT Departments.DeptCode from Departments ORDER BY DeptCode"
Set DeptList = comInv.Execute
Do While Not DeptList.EOF
If DeptList("DeptCode") = GetDetailData("TransferDept") Then
%>
<OPTION selected VALUE="<%=DeptList("DeptCode")%>"><%=DeptList("DeptCode")%>
<%
ELSE
%>
<OPTION VALUE="<%=DeptList("DeptCode")%>"><%=DeptList("DeptCode")%>
<%
End If
DeptList.MoveNext
Loop ' DeptList
%>
</SELECT>
</TR>
</TABLE>

<TABLE Align="Center">
<TR><TD><INPUT NAME="Submit" TYPE="submit" VALUE="Update"></TD>
<TD><INPUT NAME="Submit" TYPE="submit" VALUE="Delete"></TD></TR>
</TABLE>

<%
GetDetailData.MoveNext
Loop ' GetDetailData
%>
</BODY>
</HTML>