1

any help would be great. Here's what I have so far:

PHP:

<?php

$arr1 = array("1080", "939", "769", "696","572", "498", "408", "369");

$arr2 = array("1275", "1095", "890", "799", "676", "580", "472", "423");

?>

PullDown Menu:

<select name="age" id="age">
<option value="<?php echo $arr1[0]; ?>">18-24</option>
<option value="<?php echo $arr2[0]; ?>">25-29</option>
</select>

Textfield:

<input name="planA" type="text" id="planA" value="<?php echo $arr1[0]; ?>" size="10" />

I need the textfield to show the correct value from the array, based on what I choose from the pulldown menu.

Thx.

user888864
  • 25
  • 8

1 Answers1

1

When a user selects a value from the pulldown, they'll just see what you have between option tags; so for the second value, 25-29 would be displayed. According to your code, this has a corresponding value of 1275. If that's the value you need to populate in a text box, you could do so using Javascript:

<select name="age" id="age" onChange=PopulateField(this.value);>
...

And in the head of the document:

<script type="text/javascript" language="javascript">
function PopulateField(numValue)
{
    document.getElementById('planA').value = numValue;
}
</script>

Of course realize that if Javascript is disabled on the user's browser, this won't work. When the form is submitted, you'll still be able to get the value of course, by working with the age variable; its value already is set to the element from the array. (I assume the text box is just for user feedback.)

Now, if the form is already submitted, and you want to show the results of the submission in the text field, then all you need to do is change your PHP code to reflect the selected value as so:

<input name="planA" type="text" id="planA" value="<?php echo $_POST['age']; ?>" size="10" />

If your form uses GET instead of POST, change $_POST to $_GET accordingly.


Edit (per comments):

If you have multiple textboxes to populate, it kind of depends whether you have just one pulldown menu to trigger them, or if there are multiple pulldown menus. I'll assume you have another pulldown menu to populate another textbox. In that case, you can use a second javascript function like this:

<script type="text/javascript" language="javascript">
function PopulateFieldA(numValue)
{
    document.getElementById('planA').value = numValue;
}
function PopulateFieldB(numValue)
{
    document.getElementById('planB').value = numValue;
}
</script>

On the pulldown that is associated with the textfield for plan B, use the appropriate onChange event:

<select name="ageB" id="ageB" onChange=PopulateFieldB(this.value);>

You could pass multiple arguments to a function so you could populate multiple boxes:

function PopulateFields(numValueA, numValueB)
{
    document.getElementById('planA').value = numValueA;
    document.getElementById('planB').value = numValueB;
}

Anything else is just speculation on my part because I don't really know the specifics of your application. Hopefully this helps set you on the right track.

JYelton
  • 35,664
  • 27
  • 132
  • 191
  • The textfield is where I want to show the correct value from the array. I want to use the onChange so that I don't have to use a submit form. Can your line: document.getElementById('planA').value = numValue; take into account the multiple textfields? – user888864 Aug 10 '11 at 23:17
  • I wasn't aware there were multiple text fields. However you can certainly author the javascript function to populate as many as you like. – JYelton Aug 10 '11 at 23:41
  • There is only 1 pulldown menu. – user888864 Aug 11 '11 at 00:01