PDA

View Full Version : Referencing Field with Selected List Item


ntenspy
06-11-2003, 09:14 AM
Background-
I have an Access DB with one table. The Table consists of two fields, "Customer" and "CustID".
I want to create an ASP page, which includes a drop-down list and a text field (or Label). The list will be populated with the Customer Names from the DB. When a Customer is selected from the list, I want the text field (or Label) to be populated with the corresponding Customer ID Number.

Progress-
I have connected to the DB and populated the list with the Customer Names, this was not to hard for me but, when it comes to getting the correct Customer Id to show up in the text field, I'm at a complete loss.

If anyone could point me in the right direction I would be most grateful!

Thanks,
Phil T.

Angelika
06-11-2003, 11:58 AM
Play with this code.

<%@ language=Javascript %>

<%
var strFirstName = "";

var strLastName = "";

var strState = "";

if (Request.Querystring("stateChanged") == "true")

{

strFirstName = Request.Form("txtFirst");

strLastName = Request.Form("txtLast");

strState = Request.Form("cboState");

}

%>
<script language=Javascript>

function initControls()

{

document.frmTest.txtFirst.value = "<%=strFirstName%>";

document.frmTest.txtLast.value = "<%=strLastName%>";

populateState();

populateCity();

}

function populateState()

{

<%

var rs = Server.CreateObject("ADODB.Recordset");

rs.Open("select * from states","DSN=SQL;UID=....;pwd=....");

var n=0;

while (!rs.EOF)

{

var stateID = rs.Fields("stateid");

var stateDesc = rs.Fields("stateLName");

Response.Write("document.frmTest.cboState[" + n + "] = new Option('" + stateDesc+ "','" + stateID + "');");

if (new String(strState).search(stateID) != -1)

Response.Write("document.frmTest.cboState[" + n + "].selected = true;");



rs.MoveNext();

n++;

}

rs.Close();

%>

}

function populateCity()

{

<%

if (strState != "")

{

var rs2 = Server.CreateObject("ADODB.Recordset");

var strSql = "select * from table_name where stateid = '" + strState + "'";

rs2.Open(strSql, "DSN=fddf;UID=.....;pwd=.....");

var n=1;

while (!rs2.EOF)

{

var strCity = rs2.Fields("city");

Response.Write("document.frmTest.cboCity[" + n + "] = new Option('" + strCity + "','" + strCity + "');");

rs2.MoveNext();

n++;

}

rs2.Close();

}

%>

}

function fillCity()

{

document.frmTest.action = "test.asp?stateChanged=true";

document.frmTest.submit();

}

function saveData()

{

document.frmTest.action = "./nextPage.asp";

return true;

}

</script>
<html>

<body onload="initControls()">
<p align="center"><font face="Times New Roman" size="5" color="#000080">
Dependent Dropdown List Boxes</font></p>
<form name=frmTest method=post onSubmit="return saveData()">

<table>
<tr>
<td> First Name </td>
<td><input name=txtFirst size="20"></td>
<td> Last Name </td>
<td><input name=txtLast size="20"></td>
</tr>
<tr>
<td> State </td>
<td> <select name=cboState onChange="fillCity()">
<option selected value=""> </option>
</select>
</td>
<td> City </td> <td> <select name=cboCity> <option selected value=""> </option> </select></td>
</tr>
<tr>
<td><input type=submit value=Save> </td>
</tr>
</table>

</form>