[英]Array of select values
So basically, I have an array of Modules and I want to have a drop-down menue that users can then select what grade they got. 因此,基本上,我有一系列的模块,我想拥有一个下拉菜单,然后用户可以选择他们所获得的等级。 This works fine, however, I would like the results to be stored inside of an array, to however many values they selected. 这样做很好,但是,我希望将结果存储到数组中,无论它们选择了多少值。 So for example: 因此,例如:
If someone selected "40" in Mod1 and in Mod2 they selected "20" then the array would be like this: 如果有人在Mod1和Mod2中选择了“ 40”,则他们选择了“ 20”,则数组将如下所示:
mod1=>40 MOD1 => 40
mod2=>20 模2 => 20
... ...
Here is the code so far, it's probably something stupid, I just cannot get my head around it. 到目前为止,这里是代码,这可能是愚蠢的,我只是无法理解。
<?php
$modules = array('Mod1', 'Mod2', 'Mod3');
if(!isset($_POST['submitted']))
{
echo '<form method="post">';
echo 'Please enter the grades you got for each Module: <br />';
foreach($modules as $module)
{
echo $module . ': <input type="text" name="grades[]" value=""> <br />';
}
echo '<br /><input type="submit" name="submit" value="Go!">';
echo '<input type="hidden" name="submitted" value="TRUE">';
}else{
$input = $_POST['score[]'];
foreach($modules as $i => $module){
$input[$module] = $input[$i];
var_dump($input[$module] = $i);
//unset($input[$i]);
}
//var_dump($input);
}
?>
<select name="score[<?php echo $module; ?>]">
should get you going :) 应该让你走:)
the array will look exactly like you narrowed it down in the intro. 该数组看起来就像您在简介中缩小的范围。
just change your name attribute to an array: 只需将您的name属性更改为数组即可:
echo '<select name="score[]">';
the the $_POST variable will be in an array $ _POST变量将在数组中
You could use a name that groups the values of the select
s in POST
into one array: 您可以使用将POST
中的select
s的值分组为一个数组的名称:
echo '<select name="score[]">';
You can then use: 然后,您可以使用:
$input = $_POST['score[]'];
foreach($modules as $i=>$module){
$input[$module] = $input[$i];
unset($input[$i]);
}
var_dump($input);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.