开发者

How to show the values from a PHP array in a textfield, based on a dropdown menu

开发者 https://www.devze.com 2023-03-27 11:21 出处:网络
any help would be great. Here\'s what I have so far: PHP: <?php $arr1 = array(\"1080\", \"939\", \"769\", \"696\",\"572\", \"498\", \"408\", \"369\");

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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号