PHP数组和HTML表单下拉列表 [英] PHP Array and HTML Form Drop Down List

查看:99
本文介绍了PHP数组和HTML表单下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的PHP阵列名为$类别如下:

I have a simple PHP Array called $categories that looks like this:

Array
(
[Closed] => P1000
[Open] => P1001
[Pending] => P1002
[In Progress] => P1003
[Requires Approval] => P1004
)

我有,有一个下拉列表,我想使用的选项数组中的一个领域一个简单的HTML表单,但是我只希望它做的显示文本(例如:封闭式,开放式之前,在进展和需要审批),在下拉列表中的选项,但存储该选项(如P1000,P1001等),然后将它发送当表单提交一个POST值相关联的键。

I've got a simple HTML form that has a drop down list for one field that I would like to use the array for the options, however I only want it do show the text (e.g. Closed, Open, Pending, In Progress and Requires Approval) as the options in the drop down list but store the associated key for that option (e.g. P1000, P1001 etc) which is then sent as a POST value when the form is submitted.

表单域的HTML到目前为止是:

The HTML for the form field so far is:

<select name="category_id">
<option value=""></option>
<?php foreach($categories as $category) {$category = htmlspecialchars($category);?>
<option value="<?php echo $category; ?>"><?php echo $category; ?></option>
<?php
}
?>
</select>

我可以让它显示无论是文字或标识的,但不是文本并存储ID。希望这是简单的东西,有人可以点我在正确的方向。

I can get it to display either the text or the ID's but not the text and store the ID. Hopefully this is something simple that someone can point me in the right direction.

非常感谢,
史蒂夫

Many thanks, Steve

推荐答案

您已经忘记了约$价值标签。你被贴上类别名称两次,而不是价值。你应该做的:

You've forgot about $value tag. You were pasting Category name twice, instead of value. You should do that:

<select name="category_id">
<option value=""></option>
<?php 
    #                             !vCHANGEv!
    foreach($categories as $category => $value) 
    {
       $category = htmlspecialchars($category); 
       echo '<option value="'. $value .'">'. $category .'</option>';
    }
?>
</select>

这篇关于PHP数组和HTML表单下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆