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

Using onChange to fill textboxes from recordset 2

Status
Not open for further replies.

pmrankine

Programmer
Jul 18, 2001
71
GB
Firstly, apologies if the answer to this is staring me in the face but I've been trailing Google and forums for a week now and I'm a little bamboozled... (also fairly new to asp/javascript so please be gentle!)

Okay, to my problem...
I have one dropdown and 2 textboxes - all info for these are pulled from a database. Populating the dropdown is fine but I need to fill the textboxes with the rest of the record when the user selects from the dropdown. I've had a go with using the OnChange but I'm not sure the best/correct way to go about getting the values into the function - I've tried putting the recordset into an array. Here's a snippet of the code I'm using to build the dropdown:
Code:
<select size="1" name="user_full_name" class="a" onchange="setDetails()">
<option>Select Name</option>
<%
For i = 0 to UBound(aryUserDetails,2)
  strFullName = strFullName & "<option value='" & i & "'>" & aryUserDetails(0,i) & "</option>" & vbcrlf
Next
Response.Write(strFullName)
%>
</select>
I've tried:
Code:
  onchange="setDetails(aryUserDetails)"
to pass the array but basically I'm not sure what to do once I'm in the function - I've tried a few things, I thought (probably rather naively) the following would be on the right track:
Code:
function setDetails()
  {
    intAryValue = document.frm_tech.user_full_name.value;
    document.frm_tech.user_email_address.value = aryUserDetails(2,intAryValue);
  }
Sorry for going on so long. Any pointers?

pmrankine
 
First off, you cannot call an asp array from javascript as javascript runs client side and asp runs server side. Second, in order for your requirements to work, you will need to use asp on the server to write the javascript to be executed for the textboxes to update client-side.

Having said that, the javascript needed depends on the number of elements being tracked. Is the value selected in the dropdown the same value that goes in the first textbox and some additional info goes in the second textbox? Or, is the value in the dropdown cause different values to go into both the textboxes?
 
Thanks for your response mbiro,
I'm pulling 3 columns from a database:
username, telephone and email
The dropdown is populated with the username
When a username is selected, their telephone and email details are populated in the textboxes.
 
Here is some code that works. There is a shorter way to do the javascript, but I used this as it is easier to follow. You can implement this a number of different ways, but the easiest may be to just have the recordset populate the three arrays (I hardcoded them at the top) and just use the rest of the code as is).

<%
dim username(2)
dim telephone(2)
dim email(2)

username(0) = "Alice"
username(1) = "Betty"
username(2) = "Carol"

telephone(0) = "111-1111"
telephone(1) = "222-2222"
telephone(2) = "333-3333"

email(0) = "alice@home.com"
email(1) = "betty@home.com"
email(2) = "carol@home.com"
%>
<head>
<script language="JavaScript">
function textfill(item) {
<%
for x = 0 to ubound(username)
%>
if (item == <%=x+1%>) {
document.Form1.text1.value = '<%=telephone(x)%>';
document.Form1.text2.value = '<%=email(x)%>';
}
<%next%>
}
</script>
</head>
<form name="Form1">

<select name=select1 onChange="textfill(this.selectedIndex)">
<option>Select Name</option>
<%
for i = 0 to ubound(username)
%>
<option><%=username(i)%></option>
<%
next
%>
</select>
<P>
<input type=text name=text1>
<P>
<input type=text name=text2>

</form>
 
Thanks, I've tried your code - it makes sense now as I typed it in. Only problem is it's not working..
I know the arrays contain the right information:
the combo box is populated
if I view the source I see the following -
Code:
function setDetails(item)
{
  If(item == 1
    {
	document.frm_tech.user_email_address.value = 'username@letts.co.uk';
	document.frm_tech.user_tel_extension.value = '0131 663 1971 ext 250';
    }
etc, etc

I've tried stripping the function down and hard-coding the array index:
Code:
document.frm_tech.user_email_address.value = '<%=aryUserEmail(0)%>';
document.frm_tech.user_tel_extension.value = '<%=aryUserTel(0)%>';
this still doesn't give me anything but if I replace
Code:
<%=aryUserTel(0)%>
with text, the text appears (but not if I have all the original code in.)

Sorry but can you help?
 
When you view the source, do the values appear in the function setDetails? This is how view source looks on my example:

Also, did you try the example I posted as is? That should definitely work for you.


<head>
<script language="JavaScript">
function textfill(item) {

if (item == 1) {
document.Form1.text1.value = '0131 663 1971 ext 250';
document.Form1.text2.value = 'username@letts.co.uk';
}

if (item == 2) {
document.Form1.text1.value = '222-2222';
document.Form1.text2.value = 'betty@home.com';
}

if (item == 3) {
document.Form1.text1.value = '333-3333';
document.Form1.text2.value = 'carol@home.com';
}

}
</script>
</head>
<form name="Form1">

<select name=select1 onChange="textfill(this.selectedIndex)">
<option>Select Name</option>

<option>Alice</option>

<option>Betty</option>

<option>Carol</option>

</select>
<P>
<input type=text name=text1>
<P>
<input type=text name=text2>

</form>
 
Yes - works as is. As soon as I populate the arrays from a recordset, it doesn't work.

I think I'll need to push through my training - I've a huge list of new things to do/learn but the only resource I've got is the internet. Sorry, moan over.
 
Finally got it to work..
The data is being pulled from a Lotus Notes Database (which I know less about than asp and javascript) - one of the columns' multi-value property was set to 'new line'. After I changed this, problem solved - with a big thank you (and a star) for mbiro.
 
I am trying to do the same thing and came across this post. I am doing it a little different by reading the recordset into an array. My array has 5 elements. Here is my code:
Code:
<html>
<%
	Dim Connection
	Dim RecSet     ' recordset object
	Dim Row        ' current row
	Dim Col        ' current column
	Dim LastRow    ' ordinal of last row in the array
	Dim LastCol    ' ordinal of last column in the array
	Dim RecSet_Array
	Dim SQL

	SQL = "SELECT sheetno,pattern,machine,currentrev,curevdate,toolno FROM GrindsheetQ1 ;"
	
	Set RecSet = Server.CreateObject ("ADODB.Recordset") 
	RecSet.Open SQL, Connection, adOpenStatic, adLockReadOnly, adCmdText
	RecSet_Array = RecSet.GetRows()
%>	
	<SCRIPT LANGUAGE="JavaScript">
		var x;
		function FillFields(item){
				<%
					for x = 0 to UBound(RecSet_Array,2)
				%>	
				if (item == <%=x+1%>) {
        	document.form1.txtPat.value = '<%RecSet_Array(0,x)%>';
					document.form1.txtTool.value = '<%RecSet_Array(1,x)%>';
					document.form1.txtOper.value = '<%RecSet_Array(2,x)%>';
					document.form1.txtRev.value = '<%RecSet_Array(3,x)%>';
					document.form1.txtRevDate.value = '<%RecSet_Array(4,x)%>';
    		}
    		<%next%>
		}
</script>		
	RecSet.Close
	Set RecSet = Nothing
%>

<head>
<title>Cleaning Department Process Sheets and Associated Docs</title>

</head>
<body background="\gredesc\pics\ACSUMIPT.GIF">
<form action="testgrindresults.asp" method=post name="form1">
<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" width="100%">
  <tr>
    <td width = "15%">
		<a href="../index.html.en">
    <img border="0" src="\gredesc\pics\g_home.gif" width="66" height="97"></a></td>  
		<td width = "70%">
    <p align="center"><font size="6" face="Arial"><b>Cleaning Room Process Sheets</b></font>&nbsp
		<hr width="100%" size="3" color="blue"></td>
		<td width = "15%"></td>
	</tr>	
</table>
<table border="1" width="100%">
<tr>
<td width="33%">
<%
	LastCol = 2
  LastRow = UBound(RecSet_Array, 2)
  
  Response.Write "<SELECT NAME='sheetlist' SIZE=20 onChange='FillFields(this.selectedIndex)'>"
	For Row = 0 To LastRow                
    Response.Write "<OPTION value=" & RecSet_Array(0, Row) & ">"
		For Col = 0 To LastCol 
		 StringLength = Len(RecSet_Array(Col, Row))
		 NumOfSpaces = 15 - StringLength
		Response.Write RecSet_Array(Col, Row) & "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
		Next
  Next
	Response.Write "</OPTION>"
	Response.Write "</SELECT>"
%>	
</td>
<td width="66%" align="left">
Pattern:<input type="text" name="txtPat" size="16">
Tool:<input type="text" name="txtTool" size="4">
Operation:<input type="text" name="txtOper" size="22">
Revision:<input type="text" name="txtRev" size="4">
Revised Date:<input type="text" name="txtRevDate" size="8">
</td>
</table>
<br>
<input type="submit" value="View Instructions">
</form>
</body>
</html>

I am receiving the error, "type mismatch", referring to line 29 which is the following: document.form1.txtPat.value = '<%RecSet_Array[0,x]%>';

I understand what type mismatch means, but I cannot see how that applies here. What am I doing wrong here?
 
Parentheses are used to refer to array elements in VBScript, so this
Code:
<%RecSet_Array[0,x]%>
becomes
Code:
<%RecSet_Array(0,x)%>
 
I am sorry, I am using the parentheses in my code. The example that I showed contained brackets because I copied it from an old example. So, if it is not the absence of parentheses what else could be causing the type mismatch?
 
Another mistake.
When I use the parentheses in my array, I get an error saying, "cannot use parentheses when calling a sub", referring to the same line of code mentioned above. When I use brackets, that is when I get the type mismatch error. Sorry for the confusion.
 
Do you need an equals before the array name?
Code:
<%=RecSet_Array(0,x)%>

pmrankine
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top