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

Drop Dows/ Arrays and JavaScript Help Please

Status
Not open for further replies.

PaulSc

MIS
Aug 21, 2000
148
GB
Hi,
I have an asp generated form with database populated drop down(Select) boxes.

The recordset data is also added to a 2d array. This works fine, however I then want to update a couple of text boxes dependant on the selection made from the dropdown. I want this done client side (hence the use of the array)

My dropdown contains an onClick event to call a Javascript
this then is supposed to read the array and update the appropriate boxes with the correct data.

If I 'hardcode' a value in to the javascript works ok, likewise If I place the arraykey field it updates etc but I cant seem to retrieve the actual array data.

Can anybody suggest where I may be going wrong. or suggest an alternative method..

Select call =
onChange='UpdateAuth(document.NewSRD.cboAuthOfficer.value)'

javascript is : -
<SCRIPT language=&quot;javascript&quot;>
function UpdateAuth(AuthId)
{
if (document.NewSRD.cboAuthOfficer.value > 0)
{document.NewSRD.txtAuthDept.value = AuthArray(3,Authid)
NewSRD.txtAuthDept.focus()
return false;}
return true;
}
</SCRIPT>
 
Does this work? I cannot see your array, but I think javascript does not use () but [] to get the value of an array.

Select call =
onChange='UpdateAuth(this.value)'

javascript is : -
<SCRIPT language=&quot;javascript&quot;>
function UpdateAuth(AuthId)
{
if (document.NewSRD.cboAuthOfficer.value > 0)
{document.NewSRD.txtAuthDept.value = AuthArray[3,Authid]
NewSRD.txtAuthDept.focus()
return false;}
return true;
}
</SCRIPT>
 
Heh, I did this yesterday. As far as I know (and could find searching yesterday) javascript doesn't support more than one dimensional arrays unless you define them out aahead of time. Heres is the script I was playing with yesterday:
Code:
<html>
<head>
<title> Shift Selection </title>
<script language=&quot;JavaScript&quot;>
<!--
var shifts;
	function init(){
		//to initialize them all to the same times
		shifts={dayOfWeek: [{day: &quot;monday&quot;,theShifts: [&quot;1&quot;,&quot;2&quot;,&quot;3&quot;]},{day:&quot;tuesday&quot;,theShifts: [&quot;4&quot;,&quot;5&quot;,&quot;6&quot;]}]};


		alert(&quot;test: &quot;+shifts.dayOfWeek[0].day.toString());
	}

	//change day function
	function changeDay(elem){
		var j, day;
		var shiftText;

		if(elem.selectedIndex == 0)	//if they selected our pretty [select a day] stmt
			return false;			//do nothing and leave quietly
		
		//Clear the second drop down of all but top [select a shift]
		for (i = frmFormName.workday.options.length - 1; i >= 1; i--) frmFormName.workday.options[i] = null;
		frmFormName.workday.selectedIndex = 0;

		//grab day from select box
		day = elem.selectedIndex-1;

		for(j=0;j<shifts.dayOfWeek[day].theShifts.length;j++){
			document.frmFormName.workday.options[j] = new Option(shifts.dayOfWeek[day].theShifts[j],&quot;&quot;);
		}
	}
//-->
</script>
</head>
<body onLoad=&quot;init();&quot;>
<form method=POST action=&quot;wherever.asp&quot; name=&quot;frmFormName&quot;>

<select name=&quot;weekday&quot; onChange=&quot;changeDay(this);&quot;>
	<option>[Select a day]</option>
	<option>Monday</option>
	<option>Tuesday</option>
</select>
<select name=&quot;workday&quot;>
	<option>[Select a Shift]</option>
</select>
</form>
</body>
</html>
You would need to build the array dynamically, but here you go. This one only populates the second box dynamically, but I'm sure you'll see I could have populated the first dtynamically after building the array by using the day portion of the array. Hope this helps,
-Tarwn
 
No unfortunatley it didnt...

The array is available as I use the
Response.Write(AuthArray(iColLoop, iRowLoop) & &quot;<br>&quot;) to display it and have various screen fields, but when the javascript tries to access it i get &quot;AuthArray&quot; is undefined??

Would any other code I use be of any help??

Thanks for responding so quickly...

PaulSc
 
Response.Write(AuthArray(iColLoop, iRowLoop) & &quot;<br>&quot;) is asp vb code, this is executed on the Server, javascript is executed on the client so you cannot address variables declared in your asp code in client code (java, vb script or perlscript) and vica versa.

Normally I use the record ID for the value of a select option and a more recognisable field for the innerHTML of a select option.
For example: my table persons got a ID and Name field then my select is like this:
<option value='record(&quot;ID&quot;)'>record(&quot;Name&quot;)</option>

Now, when your form is submitted you wil get the ID of your persons table.
If you need to show the user the innerHTML of an opion or put this value somewhere else you can do it with client java script like this:

<script>
function myfunction(objSelect) {
var iu=0;
while(iu<objSelect.options.length) {
if(objSelect.options[iu].selected) {
selValue.innerHTML = objSelect.options[iu].innerHTML;
}
iu++;
}
}
</script>

<select name='myselect' onchange=&quot;myfunction(this);&quot;>
<option value=0></option>
<option value=3>aaa (recordID =3)</option>
<option value=5>bbb (redordID =5</option>
<option value=7>ccc (redordID =7</option>
<option value=9>ddd (redordID =1</option>
</select>
<br>
<label id=selValue></label>


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top