By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,689 Members | 1,759 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,689 IT Pros & Developers. It's quick & easy.

Pass variable selected from drop down option field to text fields

P: n/a
Dan
Can anyone offer suggestions on how to do this or if it is possible?

I have a form that uses a drop down box and 2 text fields.

What I am trying to do is have the value of each text box set by the
choice from the drop down box.

Something like:

<form name="populatefrm" id="contactfrm" method="post"
action="results.asp">

<select name="region" size="10">
<option value="Choice 1" JavaScript "SET Text Box 1 to 12:00; SET Text
Box 2 to 5:00">Choice 1</option>
<option value="Choice 2" JavaScript "SET Text Box 2 to 5:00; SET Text
Box 1 to 11:00">Choice 2</option>
</select>
<P>
<input type="text" name="text_box_1" value="value from drop down box
script" size="5"><BR>
<input type="text" name="text_box_2" value="value from drop down box
script" size="5"><BR>

<input type="submit" value="Submit">
</form>
This is related to the time question I asked below. I've searched the
web and can't find exactly what I am looking for.

Thanks, Dan
Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Dan wrote:
What I am trying to do is have the value of each text box set by the
choice from the drop down box.

Something like:

<form name="populatefrm" id="contactfrm" method="post"
action="results.asp">

<form ...>
<select name="region" onchange
="this.form['text_box_'+(this.selectedIndex+1)].value
=this[this.selectedIndex].value">
<option value="5.00" >Choice 1</option>
<option value="11.00">Choice 2</option>
<input type="text" name="text_box_1"size="5"><BR>
<input type="text" name="text_box_2" size="5">
</select>
</form>

Mick
Jul 23 '05 #2

P: n/a
Dan
Thank you for posting the script, I think my
question wasn't clear. I tried modifing the posted script but I'm
missing something.

What I would like to do is have the drop down
selection keeps it defined variable that is passed on
to database. Depending on which value from the drop
down box is selected, it will populate 2 different
text boxes with 2 different values.

Thanks for any help!

Here is the script:

<SCRIPT LANGUAGE="JavaScript">

var firstArray = new Array("('Select region','',true,true)",

//
Each field below will have 2 different time values
that will be added to the 2 text boxes.
//

"('this value selected and sent to script on submit'
value_1_for_text_box_1=5:00;
value_2_for_text_box_2=12:00;)",
"('Highway 1' value_1_for_text_box_1=11:00;
value_2_for_text_box_2=1:00;)",
"('Highway 2' value_1_for_text_box_1=7:00;
value_2_for_text_box_2=4:00;)",
"('highway 3' value_1_for_text_box_1=8:00;
value_2_for_text_box_2=22:00;)",
"('etc. for about 6 more locations')");
var secondArray = new Array("('Select region','',true,true)",
"('highway 4' value_1_for_text_box_1=3:00;
value_2_for_text_box_2=6:00;)",
"('highway 5'value_1_for_text_box_1=5:00;
value_2_for_text_box_2=17:00;)",
"('etc. for about 6 more locations')");
function populateLocation(inForm,selected) {
var selectedArray = eval(selected + "Array");

for (var i=0; i < selectedArray.length; i++) {
eval("inForm.location.options[i]=" + "new Option" +
selectedArray[i]);
}
}
</script>

<script type="text/javascript">
function addTimes(a,b,c) {
if (a.value != '') {
/* Validate input here to check time entered is
of correct hh:mm format and within required
range
Handle error if is isn't
*/
}
var t0 = a.value.split(':');
var t1 = b.value.split(':');
var t2 = c.value.split(':');

var m1 = +t0[1] + +t1[1];
var h1 = +t0[0] + +t1[0] + Math.floor(m1/60);
m1 = (m1 % 60);
b.value = h1 + ':' + m1;

var m2 = +t0[1] + +t2[1];
var h2 = +t0[0] + +t2[0] + Math.floor(m2/60);
m2 = (m2 % 60);
c.value = h2 + ':' + m2;
}
</script>
</head>

<body>

<form method="POST" action="results.asp" name="loc">

//Below is the value that needs to be passed on to the
ASP form. The value selected below will load a 2nd
drop down list that will contain the two variables for
the text boxes.
//

<select name="region"
onChange="populateLocation(document.loc,document.l oc.region.options[document.loc.region.selectedIndex].value)">
<option selected value=''>Select Region</option>
<option value='Region1'>Region1</option>
<option value='Region2'>Region2</option>
</select>

//The drop down list below is populated by the 1st
drop down list script and contains 1 variable that is
passed to the Data Base and contains the two variables
that need to be added to the 2 text boxes.
//

<select name="location">
<option value=''>Choose Region 1st</option>
</select>
<input type="text" name="location_1"
size="20"></font>
<label for="inTime">
<input type="text" name="inTime"
onblur="addTimes(this,this.form.time1,
this.form.time2)"></label>

//The time value is inserted below from your script
using values from the second set of drop down boxes.
//

<input type="text" name="time1"
value="this is the part I am trying to script1"></label>

//The 2nd time value is inserted below from your
script using values from the second set of drop down
boxes.
//

<label for="inTime"><input type="text"
name="time2" value="this is the part I am trying to script2"></label>

<input type="submit" value="Submit" name="B1">
<input type="reset" value="Reset" name="B2">
</form>
Mick White <mw***********@rochester.rr.com> wrote in message news:<Ge*****************@twister.nyroc.rr.com>...
Dan wrote:
What I am trying to do is have the value of each text box set by the
choice from the drop down box.

Something like:

<form name="populatefrm" id="contactfrm" method="post"
action="results.asp">

<form ...>
<select name="region" onchange
="this.form['text_box_'+(this.selectedIndex+1)].value
=this[this.selectedIndex].value">
<option value="5.00" >Choice 1</option>
<option value="11.00">Choice 2</option>
<input type="text" name="text_box_1"size="5"><BR>
<input type="text" name="text_box_2" size="5">
</select>
</form>

Mick

Jul 23 '05 #3

P: n/a
Dan
I'm making some progress.

I can't seem to get this to take more than 1 variable. High_Way2 will
not pass on its time value.

Anyone see any glaring errors?

Thanks again for all the help.

<script language="JavaScript">
<!--

function Trip_Time(Traffic_Results) {
if (document.Traffic_Results.CIP_Diff.value == "High_Way1") {
document.Traffic_Results.display_time.value = "2:20";

if (document.Traffic_Results.CIP_Diff.value == "High_Way1") {
document.Traffic_Results.display_time2.value = "4:30";

if (document.Traffic_Results.CIP_Diff.value == "High_Way2") {
document.Traffic_Results.display_time.value = "2:20";

if (document.Traffic_Results.CIP_Diff.value == "High_Way2") {
document.Traffic_Results.display_time2.value = "4:30";

}

}
}

}
else {
document.Traffic_Results.display_time.value = "0:00";

document.Traffic_Results.display_time2.value = "0:00";

}
}

// -->
</SCRIPT>
</HEAD>

<BODY BGCOLOR="white">

<form name="Traffic_Results">
<select name="CIP_Diff" onChange="Trip_Time()">
<option value="choose" selected>Choose</option>
<option value="High_Way1">High Way 1</option>
<option value="High_Way2">High Way 2</option>

</select>&nbsp;&nbsp;&nbsp; <font size="-1">Starting Location</font>
<BR>

Time at Location <input type=text name="reported_time" size=5
value=""><br>
ETA 1 <input type=text name="display_time" size=5 value="0:00"><br>
ETA 2 <input type=text name="display_time2" size=5 value="0:00">

</form>
Jul 23 '05 #4

P: n/a
do****@rocketmail.com (Dan) wrote in message news:<29**************************@posting.google. com>...
I'm making some progress.

I can't seem to get this to take more than 1 variable. High_Way2 will
not pass on its time value.

Anyone see any glaring errors?

Thanks again for all the help.

<script language="JavaScript">
<!--

function Trip_Time(Traffic_Results) {
if (document.Traffic_Results.CIP_Diff.value == "High_Way1") {
document.Traffic_Results.display_time.value = "2:20";

if (document.Traffic_Results.CIP_Diff.value == "High_Way1") {
document.Traffic_Results.display_time2.value = "4:30";

if (document.Traffic_Results.CIP_Diff.value == "High_Way2") {
document.Traffic_Results.display_time.value = "2:20";

if (document.Traffic_Results.CIP_Diff.value == "High_Way2") {
document.Traffic_Results.display_time2.value = "4:30";

}

}
}

}
else {
document.Traffic_Results.display_time.value = "0:00";

document.Traffic_Results.display_time2.value = "0:00";

}
}

// -->
</SCRIPT>
</HEAD>

<BODY BGCOLOR="white">

<form name="Traffic_Results">
<select name="CIP_Diff" onChange="Trip_Time()">
<option value="choose" selected>Choose</option>
<option value="High_Way1">High Way 1</option>
<option value="High_Way2">High Way 2</option>

</select>&nbsp;&nbsp;&nbsp; <font size="-1">Starting Location</font>
<BR>

Time at Location <input type=text name="reported_time" size=5
value=""><br>
ETA 1 <input type=text name="display_time" size=5 value="0:00"><br>
ETA 2 <input type=text name="display_time2" size=5 value="0:00">

</form>


You've posted so much conflicting code, hard to even guess at what
you're doing. In these instances, it's usually best to post some
(valid) HTML, with a simple description of what needs to happen.

Store the data in an object/array (same thing, really) and use the
selected option value as a 'key' to extract & process it.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>untitled</title>
<script type="text/javascript">
//<![CDATA[

var data = new Object;
data['High_Way1'] = '2:20|4:30';
data['High_Way2'] = '3:40|5:15';
data['default'] = '0:00|0:00';

function trip_time(selObj)
{
var sval = selObj.options[selObj.selectedIndex].value, //get selected
value
els = selObj.form.elements, //form elements
separator = '|', //separates data (above)
d = data[sval || 'default'].split(separator); //so, split it into an
array
els.display_time.value = d[0]; //output 1st element
els.display_time2.value = d[1]; //output 2nd element
}

//]]>
</script>
</head>
<body>
<form name="Traffic_Results">
<select name="CIP_Diff" onchange="return trip_time(this)">
<option value="" selected="selected">Choose</option>
<option value=""></option>
<option value="High_Way1">High Way 1</option>
<option value="High_Way2">High Way 2</option>
</select>
&nbsp;&nbsp;&nbsp; <font size="-1">Starting Location</font>
<br />
Time at Location <input type="text" name="reported_time" value=""
size="5" />
<br />
ETA 1 <input type="text" name="display_time" value="0:00" size="5"><br
/>
ETA 2 <input type="text" name="display_time2" value="0:00" size="5" />
</form>
</body>
</html>
Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.