PHP 数组和 HTML 表单下拉列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5652465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:06:38  来源:igfitidea点击:

PHP Array and HTML Form Drop Down List

phphtmlarraysforms

提问by Steve Kemp

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

我有一个名为 $categories 的简单 PHP 数组,如下所示:

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

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 表单,其中有一个字段的下拉列表,我想将数组用于选项,但是我只希望它显示文本(例如,已关闭、打开、待处理、进行中和需要Approval) 作为下拉列表中的选项,但存储该选项的关联键(例如 P1000、P1001 等),然后在提交表单时将其作为 POST 值发送。

The HTML for the form field so far is:

到目前为止,表单字段的 HTML 是:

<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>

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.

我可以让它显示文本或 ID,但不能显示文本并存储 ID。希望这是一些简单的事情,有人可以指出我正确的方向。

Many thanks, Steve

非常感谢,史蒂夫

回答by Robik

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

你已经忘记了 $value 标签。您粘贴了两次类别名称,而不是值。你应该这样做:

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

回答by Shakti Singh

As you have to include key and value both key is to show the text and value for POST

由于您必须包含键和值,这两个键都是为了显示 POST 的文本和值

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

回答by Nazariy

you have not included keys in foreach loop

你没有在 foreach 循环中包含键

foreach($categories as $id=>$category){
    $category = htmlspecialchars($category);
    echo "<option value="{$id}">{$category}</option>";
}

回答by MacMac

foreach($categories as $category => $category_id)

Is what you're looking for.

是你要找的。

回答by Muhammad Yasir

<select name="category_id">
<option value=""></option>
<?php
$keys = array_keys($categories);
for($i=0; $i<count($categories); $i++)
{?>
    <option value="<?php echo $keys[$i]; ?>"><?php echo $categories[$i]; ?></option>
<?php
}
?>
</select>

Supposing that $categories is the array you have shown above.

假设 $categories 是您在上面显示的数组。