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

JavaScript populate form fields within same page

Status
Not open for further replies.

terrahawk

Technical User
Dec 16, 2001
53
US
Hi, I was wondering if someone can help me with my script. Within the same page (using JavaScript), I would like publish values to form fields based upon user selection from a drop down list. You see similar examples for simple JavaScript shopping carts.

<!--- IN HEAD: --->

<script language="JavaScript" type="text/JavaScript">
<!--
function zip()
{
if(document.meddentrates.zip.value=="1") bcbsemployee=="50";
}
//-->
</script>

<!--- IN BODY: --->

<form name="meddentrates" id="meddentrates">

<table><tr>td>
Work Zip Code:
</td><td>
<select name="zip" id="zip">
<option value="0">Select an Office Location</option>
<option value="2">AL - Birmingham (35244)</option>
<option value="1">AL - Macon County (36083)</option>
<option value="3">AZ - Phoenix (85029)</option>
</select></td></tr>

<td>
<input type="submit" name="Submit" value="Display Rate" onclick="zip()" />
</td>

<!--- THEN BASED ON WHICH ZIP CODE THE USER SELECTS,
POPULATE THE FIELD "bcbsemployee" WITH A VALUE - in this case I choose 50. ---->

<tr>
<td>BCBS Plan</td>
<td><input name="bcbsemployee" type="text" id="bcbsemployee"/></td>
</tr>

In this case, if the user selects a zip code from the drop down list with a value of "1", how do I populate the "bcbsemployee" field with a value of 50? I have been playing around with my function and syntax, etc. but have not got it to work yet.

I have a follow-up question as well. I want to include if statements. So publish a set value to "bcbsemployee" based upon zip code and work status (radio button) and salary (drop down) list.

If "zip" == "1" & "status = "1" & salary = "1" THEN field "bcbsemployee" is populated with 50.

If "zip" == "1" & "status = "2" & salary = "2" THEN field "bcbsemployee" is populated with 75.

etc. I am not sure if I would have to write a seperate function for each of my if statements or now.

Thank-you for any help, Tony


 
You can use either nested if statements, or nested switch statements.

Using switch would be something like:

var bcbsemployee = document.forms['meddentrates'].elements['bcbsemployee'];
switch (zip)
{
case 1:
{
switch (status)
{
case 1:
{
switch (salary)
{
case 1: bcbsemployee = 50; break;
}
break;
}
case 2:
{
switch (salary)
{
case 2: bcbsemployee = 75; break;
}
break;
}
}
break;
}

and using nested if would be something like:

var bcbsemployee = document.forms['meddentrates'].elements['bcbsemployee'];
if (zip == 1)
{
if (status == 1)
{
if (salary == 1) bcbsemployee = 50;
else if (salary==2) //do something else here
}
else if (status == 2)
{
if (salary == 1) //do something else here
else if (salary==2) bcbsemployee = 75;
}
}
else if (zip == 2)
{
// follow same logic as above
}

Lee
 
Thanks Lee, I will try out those two ways.
Tony
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top