如何基于下拉菜单在文本字段中显示 PHP 数组中的值
任何帮助都会很棒。这是我到目前为止所拥有的:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当用户从下拉列表中选择一个值时,他们只会看到
option
标记之间的内容;因此对于第二个值,将显示25-29
。根据您的代码,其对应值为1275
。如果这是您需要在文本框中填充的值,您可以使用 Javascript 来实现:在文档的头部:
当然要意识到,如果在用户的浏览器上禁用 Javascript,则这将不起作用。提交表单后,您仍然可以通过使用
age
变量来获取该值;它的值已经设置为数组中的元素。 (我假设文本框只是用于用户反馈。)现在,如果表单已经提交,并且您想在文本字段中显示提交的结果,那么您需要做的就是更改您的 PHP 代码以反映所选值,如下所示:
如果您的表单使用 GET 而不是 POST,请将
$_POST
相应更改为$_GET
。Edit (per comments):
如果您有多个文本框要填充,这取决于您是否只有一个下拉菜单来触发它们,或者是否有多个下拉菜单。我假设您有另一个下拉菜单来填充另一个文本框。在这种情况下,您可以使用第二个 JavaScript 函数,如下所示:
在与计划 B 的文本字段关联的下拉菜单中,使用适当的
onChange
事件:您可以将多个参数传递给函数,以便您可以填充多个框:
其他任何内容都只是我的猜测,因为我并不真正了解您的应用程序的具体情况。希望这可以帮助您走上正确的道路。
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 of1275
. If that's the value you need to populate in a text box, you could do so using Javascript:And in the head of the document:
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:
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:
On the pulldown that is associated with the textfield for plan B, use the appropriate
onChange
event:You could pass multiple arguments to a function so you could populate multiple boxes:
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.