如何基于下拉菜单在文本字段中显示 PHP 数组中的值

发布于 2024-11-29 03:06:24 字数 661 浏览 0 评论 0原文

任何帮助都会很棒。这是我到目前为止所拥有的:

PHP:

<?php

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

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

?>

下拉菜单:

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

文本字段:

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

我需要文本字段根据我从下拉菜单中选择的内容来显示数组中的正确值。

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故事和酒 2024-12-06 03:06:24

当用户从下拉列表中选择一个值时,他们只会看到 option 标记之间的内容;因此对于第二个值,将显示25-29。根据您的代码,其对应值为1275。如果这是您需要在文本框中填充的值,您可以使用 Javascript 来实现:

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

在文档的头部:

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

当然要意识到,如果在用户的浏览器上禁用 Javascript,则这将不起作用。提交表单后,您仍然可以通过使用 age 变量来获取该值;它的值已经设置为数组中的元素。 (我假设文本框只是用于用户反馈。)

现在,如果表单已经提交,并且您想在文本字段中显示提交的结果,那么您需要做的就是更改您的 PHP 代码以反映所选值,如下所示:

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

如果您的表单使用 GET 而不是 POST,请将 $_POST 相应更改为 $_GET


Edit (per comments):

如果您有多个文本框要填充,这取决于您是否只有一个下拉菜单来触发它们,或者是否有多个下拉菜单。我假设您有另一个下拉菜单来填充另一个文本框。在这种情况下,您可以使用第二个 JavaScript 函数,如下所示:

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

在与计划 B 的文本字段关联的下拉菜单中,使用适当的 onChange 事件:

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

您可以将多个参数传递给函数,以便您可以填充多个框:

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

其他任何内容都只是我的猜测,因为我并不真正了解您的应用程序的具体情况。希望这可以帮助您走上正确的道路。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文