<form action="a.php" method="post">
<select id="sel_1" name="sel[]" multiple>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<select id="sel_2" name="sel[]" multiple>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<input type="submit"/>
</form>
现在,当我尝试像这样的数据时,
$offer = $_POST['sel'];
print_r($offer);
显示如下数据:
Array
(
[0] => 1 // 1, 2 selected for sel_1
[1] => 2
[2] => 2 // 2, 3 selected for sel_2
[3] => 3
)
不应该这样吗?
Array
(
[0] => Array(
[0] => 1
[2] => 2
)
[1] => Array(
[0] => 2
[2] => 3
)
)
我想创建这样的字符串数据(在nxt a.php文件中):
for sel_1 data is created like "1, 2";
for sel_2 data is created like "2, 3";
如何以上述格式获取数据。
我正在尝试这个
for($i = 0; $i<count($offer) ; $i++)
{
for($j = 0; $j<count($offer[$i]); $j++)
{
$string = $tring. $offer[$i][$j];
}
}
答案 0 :(得分:1)
设置名称sel1[]
和sel2[]
(不同)。在PHP中,您可以使用array_merge
来获取另一个数组,其中包含第一个和第二个数组中的值:
$offer = array_merge($_POST['sel1'], $_POST['sel2']);
$string = '';
for($i = 0; $i < count($offer); $i++)
{
$string .= $offer[$i];
}
答案 1 :(得分:1)
尝试这样,它按照您的要求运作:
而不是这个
$offer = $_POST['sel'];
像这样
$offer[] = $_POST['sel'];
<强>代码: - 强>
<form action="" method="POST">
<select id="sel_1" name="sel1[]" multiple>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<select id="sel_2" name="sel2[]" multiple>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<input type="submit"/>
</form>
<?php
error_reporting(0);
$offer=array();
$offer[] = $_POST['sel1'];
$offer[]= $_POST['sel2'];
echo "<pre>";
print_r($offer);
echo "</pre>";
?>
输出请点击此处:Output
答案 2 :(得分:0)
试试这个:
<form action="a.php" method="post">
<select id="sel_1" name="sel[1][]" multiple>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<select id="sel_2" name="sel[2][]" multiple>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<input type="submit"/>
</form>
为我工作