Wrox Home  
Search P2P Archive for: Go

  Return to Index  

asp_databases thread: pulldown lists from database


Message #1 by lodris@m... on Mon, 21 May 2001 20:29:59
I was wondering if there is a way to select an option in one pulldown list 

(whose contents is populated from the database) and then when that user 

selects that option to automatically fill in the rest of the fields that 

are related to that row in the table.For example if i choose a product and 

the next field is price which is currently blank is there a way depending 

on which product i choose to automatically fill the price text box with 

data from the database.

Some sample code or suedocode would be great if you could.

Thanks

Leigh

Message #2 by "Peter Foti (PeterF)" <PeterF@S...> on Mon, 21 May 2001 16:14:38 -0400
Leigh,



In order for the data to be filled in immediately, you would have to be

using client side scripting.  You could do this using server side

scripting, but it would require a page refresh (via submitting a form

for example).  The change is not instantanious, and I don't think this

is what you're looking for.



One way you might be able to accomplish this would be to get all of the

records when the page loads, and store them in javascript variables.

However, this will most likely cause your application to slow way down,

because it has to pull all of the information from the database and 

then

write it all to your page (in the form of a javascript script).  Also,

if you had an enormous amount of data, you could actually run out of

memory in the browser.  But here's what I mean.



When your page loads, some server side code (ASP) runs to get all of 

the

records you could possibly need from the database.  Next, you write a

javascript <script> tag in your document.  Next, you loop through the

recordset (keeping a count using a variable i) and write your data in

your document as javascript variables.  For example:

(before the loop):

Response.Write("var priceArray =3D new Array();\n")

(in the loop)

Response.Write("priceArray[" & i & "] =3D " & Chr(34) & oRS("PRICE") &

Chr(34) & ";\n")



Your generated javascript code should then look something like this:

var priceArray =3D new Array();

priceArray[0] =3D "$49.99";

priceArray[1] =3D "$39.96";

priceArray[2] =3D "$11.94";



and so on.  Note, I'm not sure how your "price" is stored in the

database, so the $ might not be stored here.



Anyway, then you could create a javascript function to update the text

boxes on your page when the onchange event is triggered on the select

box.

<script language=3D"javascript" type=3D"text/javascript">

function getRecords(optionVal)

{

	var idx =3D 0;



	// Find the index of the item selected in the array

	// so we know which index to use on all the other arrays

	for(i =3D 0; i < optionArray.length; i++)

	{

		if(optionArray[i] =3D=3D optionVal)

		{

			idx =3D i;

			break;

		}

	}



	// Now idx holds the index to all the array items

	// so we can fill in the form from our arrays

	document.forms[0].price.value =3D priceArray[idx];

	document.forms[0].weight.value =3D weightArray[idx];

	// and so on

}

</script>



And your select tag would look something like this:

<select onchange=3D"javascript:getRecord(this.value);">



Anyway, not sure if all of this helps or not.  Good luck.

Pete



PS- Note that I did not test any of this code before sending this, so

I'm not 100% sure all of the syntax is correct, but I think it is.



> -----Original Message-----

> From: lodris@m... [mailto:lodris@m...]

> Sent: Monday, May 21, 2001 8:30 PM

> To: ASP Databases

> Subject: [asp_databases] pulldown lists from database

>

>

> I was wondering if there is a way to select an option in one

> pulldown list

> (whose contents is populated from the database) and then when

> that user

> selects that option to automatically fill in the rest of the

> fields that

> are related to that row in the table.For example if i choose

> a product and

> the next field is price which is currently blank is there a

> way depending

> on which product i choose to automatically fill the price

> text box with

> data from the database.

> Some sample code or suedocode would be great if you could.

> Thanks

> Leigh

>

Message #3 by "Charles Feduke" <webmaster@r...> on Mon, 21 May 2001 16:08:41 -0400
	Depends if you want to interact with the server after they choose the item.

If its not alot of data (say less than 100

records) then you could do everything client side.  For example, you produce

HTML:



<%

	' populate rsData up here

%>

<SCRIPT LANGUAGE="JavaScript">

	var sArray = new Array(

<%

Dim sOptions, iCount



Do Until rsData.EOF ' assumes rsData has recordset data

	' set the option

	sOptions = sOptions & "<OPTION VALUE=""" & iCount & _

		">" & rsData("ProductName") & "</OPTION>" & vbCrLf

	' output the javascript - we create a JScript array here

	If iCount <> 0 Then

		' we need a comma

		Response.Write "," & vbCrLf

	End If

	Response.Write """ & rsData("ProductName") & ":" & rsData("SKU") & _

		":" & rsData("Price") & """

	' increment iCount

	iCount = iCount + 1

Loop

%>

	);

	// our function

	function changeValues(iID)

	{

		var sItems = sArray[iID].split(':');

		' sArray[0] is the product name

		document.forms[0].sku.value = sArray[1];

		document.forms[0].price.value = sArray[2];

	}

</SCRIPT>

<!-- now our form -->

<FORM METHOD="POST" ACTION="somePage.asp">

	Product: <SELECT NAME="product"

onChange="changeValues(document.forms[0].product.value);">

		<%=sOptions%>

	</SELECT><BR>

	SKU: <INPUT TYPE="text" NAME="sku"><BR>

	Price: <INPUT TYPE="text" NAME="price">

</FORM>



	The other option would be to make another request to the server for the

pertient data.  Of course, if you have a small amount of fairly static data,

the above example would work quite well.



	This is all OTTOMH so I hope it helps.



? Chuck



> -----Original Message-----

> From: lodris@m... [mailto:lodris@m...]

> Sent: Monday, May 21, 2001 8:30 PM

> To: ASP Databases

> Subject: [asp_databases] pulldown lists from database

>

>

> I was wondering if there is a way to select an option in one

> pulldown list

> (whose contents is populated from the database) and then when that user

> selects that option to automatically fill in the rest of the fields that

> are related to that row in the table.For example if i choose a

> product and

> the next field is price which is currently blank is there a way depending

> on which product i choose to automatically fill the price text box with

> data from the database.

> Some sample code or suedocode would be great if you could.

> Thanks

> Leigh

  Return to Index