1
我有这种形式,我需要提交一些隐藏值的问题,并将它们连接在一起。表单提交处理和生成
我现在追加名称与问题的ID,然后添加_然后答案的数量。
<form>
<span>1:Question goes here </span>
<input type=hidden name=t_1 value=10>
<input type=hidden name=f_1 value=20>
<input type=checkbox name=a1_1>value1
<input type=checkbox name=a1_2>value2
<input type=checkbox name=a1_3>value3
<span>2:Question goes here </span>
<input type=hidden name=t_2 value=40>
<input type=hidden name=f_2 value=20>
<select name=a2_1>
<option>blah</option>
<option>etc</option>
</select>
</form>
在服务器端,我爆炸提交的领域和把它们放在一起:
foreach ($_POST as $var => $val) {
switch ($var[0]) {
case "a" :
$b = substr($var, 1); // remove first char to get number following
$pos = strpos($b, "_");
if ($pos !== false) {
$i = explode("_", $b); // separating question number from choice number (for multi select questions)
$answer[$i[0]][] = $val;
break;
}
else {
$answer[$b][] = $val;
break;
}
case "t" :
$b = substr($var, 1);
$target[$b] = $val;
break;
}
}
有没有更好的方式来做到这一点?