我在我的表单中有以下编码.如何获取提交内部循环的所有单选按钮值的值?或者给我任何其他解决方案.
<form action="res.PHP" method="post"> <?PHP for($i=1;$i<=5;$i++) { ?> <div class="well well-sm well-primary"> <input type="hidden" name="ques"/>Questions? </div> <div class="well well-sm"> <div class="radio"> <label> <input type="radio" name="optradio<?PHP echo $i; ?>" value="a">Option 1 </label> </div> <div class="radio"> <label> <input type="radio" name="optradio<?PHP echo $i; ?>" value="b">Option 2 </label> </div> <div class="radio"> <label> <input type="radio" name="optradio<?PHP echo $i; ?>" value="c">Option 3 </label> </div> </div> <?PHP } ?> <button type="submit" class="btn btn-success" name="submit">Finish</button> </form>
使用单选按钮数组如下
<form method="post"> <?PHP for($i=1;$i<=5;$i++) { ?> <div class="well well-sm well-primary"> <input type="hidden" name="ques"/>Questions? </div> <div class="well well-sm"> <div class="radio"> <label> <input type="radio" name="optradio[<?PHP echo $i; ?>]" value="a">Option 1</label> </div> <div class="radio"> <label> <input type="radio" name="optradio[<?PHP echo $i; ?>]" value="b">Option 2</label> </div> <div class="radio"> <label> <input type="radio" name="optradio[<?PHP echo $i; ?>]" value="c">Option 3</label> </div> </div> <?PHP } ?> <button type="submit" class="btn btn-success" name="submit">Finish</button> </form>
要访问已发布的值,您只需使用$_POST [‘optradio’]
考虑选择5个问题为选项1,选项2,选项3,选项1,选项2
POST [‘optradio’]将给出数组
Array ( [1] => a [2] => b [3] => c [4] => a [5] => b )
要从此数组访问sigle值,可以使用foreach循环,
<?PHP foreach($_POST['optradio'] as $option_num => $option_val) echo $option_num." ".$option_val."<br>"; ?>