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

Update Text Fiels

Status
Not open for further replies.

llldnylll

Technical User
May 21, 2003
89
Hi,

I have a list menu that its source is my DB.

How can I make that fill in blank text fields, depending on which name on my list menu is selected??
 
That depends. I assume you want to have a list box (select) that you populate from the dropdown that, when an option is selected, forces several other text fields to be filled with related data, not what was in the option field.

You can either output all of your related data into a javascript array and use the onChange event of the select dropdown to grab the related data and assign it o the textboxes, or you can use the onChange of the select dropdown to send a form submit and fill the data in on the server in your ASP code.


Example of First method:
Code:
'assume rsData is your recordset

'1 - create a function to assign value(s) to input(s)
%>
<script language="javascript">
function AssignValues(index){
   var elem = document.getElementbyId("MyTextBox1");
   elem.value = dataArray[index][0];

   elem = document.getElementbyId("MyTextBox2");
   elem.value = dataArray[index][1];

   elem = document.getElementbyId("MyTextBox3");
   elem.value = dataArray[index][2];
}

<%
'2-create the javascript array
%>
var dataArray = new Array(<%
   Do Until rsData.EOF
      Response.Write "new Array('" & rsData("fldName2") & "','" & rsData("fldName3") & "','" & rsData("fldName4") & "'),"
      rsData.Movenext
   Loop
   %>);
</script>

<%
'...and later in your form

'3- create the select
Response.Write "<select name=""selSomething"" onChange=""AssignValues(this.selectedIndex);"">"
rsData.MoveFirst
Do Until rsData.EOF
   Response.Write "<option value=""" & rsData("fldName1") & """>" & rsData("fldName1") & "</option>"
   rsData.MoveNext
Loop
Response.Write "</select>"

Response.Write "<input type=""text"" name=""MyTextBox1""/>"
Response.Write "<input type=""text"" name=""MyTextBox2""/>"
Response.Write "<input type=""text"" name=""MyTextBox3""/>"

And an Example of the 2nd method:
Code:
<%
'assuming you have a recordset called rsData for the options and a connection called connData

'1-create the select
Response.Write "<select name=""selSomething"" onChange=""formName.submit()"">"
rsData.MoveFirst
Do Until rsData.EOF
   Response.Write "<option value=""" & rsData("fldName1") & """
   If Request.Form("selSomething") = rsData("fldName1") Then Response.Write " selected=""selected"""
   Response.Write ">" & rsData("fldName1") & "</option>"
   rsData.MoveNext
Loop
Response.Write "</select>"

'2 - create the text boxes
Dim val1, val2, val3
If Request.Form("selSomething") <> "" Then
   Dim rsVals
   Set rsVals = connData.Execute("SELECT fldName2, fldName3, fldName4 FROM MyTableName WHERE fldName1 = '" & Request.Form("selSomething") & "'")
   If Not rsVals.EOF Then
      vals1 = rsVals("fldName1")
      vals2 = rsVals("fldName2")
      vals3 = rsVals("fldName3")
   End If
   Set rsVals = Nothing
End If

Response.Write "<input type=""text"" name=""MyTextBox1"" value=""" & val1 & """/>"
Response.Write "<input type=""text"" name=""MyTextBox2"" value=""" & val2 & """/>"
Response.Write "<input type=""text"" name=""MyTextBox3"" value=""" & val3 & """/>"
%>

Warning: As these were just examples and I wrote them on the fly, they probably won't work as a cut and paste, but they should show the concept of the two methods I outlined.

hopefully these will prove useful. if you have any additional question about either method, feel free to ask,

-T

 
Thanks!

I tried to copy and paste but no luck for me :(

I am using a access db with the data (RS_Data).

On the select list I have:
<select name="select">
<%
while (!RS_Data.EOF) {
%>
<option value="<%=(RS_Data.Fields.Item("TechName").Value)%>"><%=(RS_Data.Fields.Item("TechName").Value)%></option>
<%
RS_Data.MoveNext();
}
if (RS_Data.CursorType > 0) {
if (!RS_Data.BOF) RS_Data.MoveFirst();
} else {
RS_Data.Requery();
}
%>
</select>


How can I have fields:
<input name="Phone" type="text" id="Phone">
<input name="email" type="text" id="email">

get data depending on what was selected on list menu?
 
You should be able to use either of the examples above as a template to do that. The only difference will be the names of your variables, db fields, etc. The basic concept of either of them should work for you if you follow the same logic I laid out.

First you wantto pick which method you prefer, either the client-side one or the server-side postback one. After that you want to make sure you recreate each of the major blocks of code using your own code, DB fields, etc. If you still have problems once you get that far, paste you code back here and we can assist with any errors that are showing up.

-T

 
I tried to copy and paste

Try disabling this feature of windows and you'll get farther in your programming career

[sub]____________ signature below ______________
The worst mistake you'll ever make is to do something simply the way you know how while ignoring the way it should be done[/sub]
 
Tarwn said:
they [!]probably won't work as a cut and paste[/!], but they should show the concept

llldnylll said:
I tried to copy and paste but no luck for me

[hammer]

Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top