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!

Drop Down Field to populate a text field 1

Status
Not open for further replies.

shawkz

Programmer
Oct 25, 2005
84
GB
Hi i have an array which contains station names and locations..

I want to be able to select a station from a drop down list and the textfield automatically fill in with the location.

example array

Station|Location
Sales|Office
Repair|Warehouse
Repair2|Warehouse
Admin|Office (reception)

Can anyone help?
 
You could do this fairly easily by generating a javascript array similar to chained dropdons, but obviously limited to one pair of values rather a value and multiple related values.
If you can create the array then basically all you need to do is use the OnChange event of the select dropdown to take the associated value and plkace somewhere (like a text input or a span)

So you would want to generate a javascript array that looked something like:
Code:
<script language="javascript">
var data = new Array();
data['Sales'] = "Office";
data['Repair'] = "Warehouse";
data['Repair2'] = "Warehouse";
...etc
</script>

Then for your dropdown you could do something like:
Code:
<select name="selStation" onChange="document.getElementById('mySpan').innerText = data[this.value]">
   <option value="Sales">Sales</option>
   <option value="Repair">Repair</option>
   <option value="Repair2">Repair2</option>
   ...etc
</select>
<span id="mySpan"></span>

I wrote that on the fly, but the concept should be sound. I am assuming you don't need asistance writing the array or options from your data in ASP, but let us know if you do, or if this answer doesn't fit your needs,

-T

 
Hi thanks for the reply, how can i do this with a vbscript array...

i have an array called rsStationData(0,i)

rsStationData(0,i) is the station (drop down menu)
rsStationData(1,i) is the location

so on a onchange event from a drop down menu i want to set the text field to rsStationData(1,i)

Hope this makes sence?!!!

 
Client-side VBScript or server-side (ASP) VBScript?

 
Then what you will want to do is use that server-side array to Response.Write the javascript array and select options. Something along the lines of:
Code:
'Output Javascript Array
Response.Write "<script language=""javascript"">" & vbCrLf
Response.Write "var data = new Array();" & vbCrLf

Dim rowCtr
For rowCtr = 0 To UBound(rsStationData,2)
   Response.Write "data[""" & rsStationData(0,rowCtr) & """] = """ & rsStationData(1,rowCtr) & """;" & vbCrLf
Next
Response.Write "</script>" & vbCrLf

Since the javascript is executed on the client, you can output it exactly as if you were outputting HTML or anything else you plan on the client's browser figuring out for itself.

-T

 
Thanks for the reply...

this is my code...

<select name="CallerName" id="select2" onChange="document.getElementById('Contact').value = '<%=rsCallerDetailData(1,i)%>'">
<option value=""></option>
<%
while i < UBound(rsCallerDetailData,2)
%>
<option value="<%=rsCallerDetailData(0,i)%>"><%=rsCallerDetailData(0,i)%></option>
<%
i=i+1
wend
%>
</select>

i want to be able to set the contact value to the right array element?

Please can anyone advise?

Kindest regards,

Alan
 
I am only based on your last post and guessing what you mean... You can put value of the option being (1,i) and the text of the option (0,i), then the client-side onchange will pick up the setting dynamically.
[tt]
<select name="CallerName" id="select2" onChange="document.getElementById('Contact').value = [blue]this.value[/blue]">
<option value=""></option>
<%
while i < UBound(rsCallerDetailData,2)
%>
<option value="<%=rsCallerDetailData([blue]1[/blue],i)%>"><%=rsCallerDetailData(0,i)%></option>
<%
i=i+1
wend
%>
</select>
[/tt]
Is that what you want? Note that the impact of the change is that it is the data (1,i) being post back rather than (0,i).

If that is _not_ what you want, you can still keep your original but populate a global array, say var g_array[], with (1,i)'s, and then the onchange handler will read the selectedIndex of the select-one element as the index for that global array on the right-hand-side of the assignment to the "Contact".
[tt] onChange="document.getElementById('Contact').value = g_array[this.selectedIndex]"[/tt]
 
Thanks, i see what you mean, i just want to set a textfield to an array value when a drop down list item is selected..

i.e.. If i select a value in the drop down list which is <%=rsCallerDetailData(0,i)%> then i want to set the text field to <%=rsCallerDetailData(1,i)%>

both fields values will be sumitted in the form?

Hope this help!,

Regards,

SH
 
Okay, I see. This is the way I would recommend, for it simplicity and cross-browser applicability.
[tt]
<select name="CallerName" id="select2" onChange="document.getElementById('Contact').value = this.options[this.selectedIndex].getAttribute('x')">
<option value="" x=""></option>
<%
while i < UBound(rsCallerDetailData,2)
%>
<option value="<%=rsCallerDetailData(0,i)%>" x="<%=rsCallerDetailData(1,i)%>"><%=rsCallerDetailData(0,i)%></option>
<%
i=i+1
wend
%>
</select>
[/tt]
Give the custom attribute x a more meaningful name to your liking.
 
Perfect!!! Your worth a star!!!

Kindest thanks,

SH
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top