HTML - 从多个单选按钮获取输出

时间:2016-06-15 17:27:45

标签: php html forms

我在我的表单中有以下代码。如何获取提交内部循环的所有单选按钮值的值?或者给我任何其他解决方案。 P.S:$ questionID的值是不连续的值,如:1,4,6等

echo '<form name = "checkans.php" action = "checkans.php" method = "POST" >' ;

while($row = $result->fetch_object() )  {
    $i = 0;
    $quesID = $row->questionid ;
    $question = $row->question ;
    echo "<div  id = $row->questionid name = boxs>" ;
    echo "<h2> $row->subject </h2> <br> " ; 
    //echo "<a href = 'dsdd'>f</a>";

    //  echo '<caption>Delete</caption>' ;

    //echo $row->questionid . "   " . $row->question ;
    $i++;

    $quesID = $row->questionid ;
    $query2 = "SELECT option1,option2,option3,option4 FROM options WHERE questionid='$quesID'" ;
    if( $result2 = $mysqli->query($query2) )  {
        if($row2 = $result2->fetch_object() )  {
            $option1 = $row2->option1 ;
            $option2 = $row2->option2 ;
            $option3 = $row2->option3 ;
            $option4 = $row2->option4 ;
        ?>

           <div id = "<?echo $quesID ; ?>" name = "<?echo $quesID ; ?>" >
                <b> <? echo $quesID ."  " . $question ; ?> </b>
                <br>
                <input type = "hidden" name = "quesID[]" id = "<? echo $quesID ; ?>" value = "<? echo $quesID ; ?>" >
                <input type = "radio" name = "ans[<? echo $quesID ; ?>]"  value = "1" > 
                <label> <? echo $option1 ;?></label>

                <input type = "radio" name = "ans[<? echo $quesID ; ?><? echo $quesID ; ?>]<? echo $quesID ; ?>" id = "<? echo $quesID ; ?>" value = "2" > 
                <label> <? echo $option2 ;?></label>

                <input type = "radio" name = "ans[<? echo $quesID ; ?>]<? echo $quesID ; ?>" id = "<? echo $quesID ; ?>" value = "3" > 
                <label> <? echo $option3 ;?></label>

                <input type = "radio" name = "ans[<? echo $quesID ; ?>]" id = "<? echo $quesID ; ?>" value = "4" >
                <label> <? echo $option4 ;?></label>
                <br>
           </div>

这是我提交答案的“checkans.php”

$id = $_POST['quesID'] ;
$ans = $_POST['ans'] ;

foreach ($id as $key=>$n) {
    echo $n . " " . $key ."<br/>";
    echo "quesid " . $n . "ans " . $ans[$n] . "<br/>";
}

当“$ questionID”的值不连续时,如1,4,6等,这不起作用。

建议解决方案并善解释我的代码有什么问题。

1 个答案:

答案 0 :(得分:0)

试试这可能对你有帮助..

&#13;
&#13;
<input type="hidden" name="quesID[]" id="<? echo $quesID ; ?>" value="<? echo $quesID ; ?>">
<input type="radio" name="ans[<? echo $quesID ; ?>]" value="1">
<label>
  <? echo $option1 ;?>
</label>

<input type="radio" name="ans[<? echo $quesID ; ?>]" id="<? echo $quesID ; ?>" value="2">
<label>
  <? echo $option2 ;?>
</label>

<input type="radio" name="ans[<? echo $quesID ; ?>]" id="<? echo $quesID ; ?>" value="3">
<label>
  <? echo $option3 ;?>
</label>

<input type="radio" name="ans[<? echo $quesID ; ?>]" id="<? echo $quesID ; ?>" value="4">
<label>
  <? echo $option4 ;?>
</label>
&#13;
&#13;
&#13;