如何使这个表单的输出输入成为一个数组?

时间:2021-11-10 21:19:08

I'm trying to create a form in which users can select multiple options from a drop down menu. This form looks something like the following:

我想创建一个表单,用户可以从下拉菜单中选择多个选项。这个表单如下所示:

<html>

<form method='post'>
<select name='tag' multiple>
<option value='opt1'>Option 1</option>
<option value='opt2'>Option 2</option>
<option value='opt3'>Option 3</option>
<option value='opt4'>Option 4</option>
<option value='opt5'>Option 5</option>
</select>
<input type='submit' Value='Submit'>
</form>

<? include('select.php'); ?>

</html>

Where the php file contains the following simple code:

其中php文件包含以下简单代码:

<?php

if($_POST){

$tag = $_POST['tag'];
echo $tag;

}

?>

The end result of this code is a drop down menu from which you may select multiple options. However, when you click submit, it only echos one of the options that the user selected.

此代码的最终结果是一个下拉菜单,您可以从中选择多个选项。但是,当您单击submit时,它只会响应用户选择的选项之一。

How can I make an array of all of the options selected by the user?

如何为用户选择的所有选项创建一个数组?

3 个解决方案

#1


13  

Try Changes the to <select name='tag' multiple> to

尝试更改

<select name='tag[]' multiple>

For PHP side :

PHP的一面:

foreach ($_POST['tag'] as $selectedOption){
    echo $selectedOption."\n";
}

#2


0  

<select name='tag[]' multiple>

#3


0  

You can change your select tag from this to this:

您可以将选择标签从这个改为这个:

<select name='tag[]' multiple>

In PHP:

在PHP中:

foreach ($_POST['tag'] as $option_selected){
    echo $option_selected;
}

#1


13  

Try Changes the to <select name='tag' multiple> to

尝试更改

<select name='tag[]' multiple>

For PHP side :

PHP的一面:

foreach ($_POST['tag'] as $selectedOption){
    echo $selectedOption."\n";
}

#2


0  

<select name='tag[]' multiple>

#3


0  

You can change your select tag from this to this:

您可以将选择标签从这个改为这个:

<select name='tag[]' multiple>

In PHP:

在PHP中:

foreach ($_POST['tag'] as $option_selected){
    echo $option_selected;
}