2014-10-20 97 views
-1

我正在開發在線測試應用程序,顯示所有的問題在一個頁面options.how我可以得到所有選擇的單選按鈕提交與問題ID的值。如何獲取在php中提交所有單選按鈕的選定值?

<?php 
include_once('connect.php'); 
$sql="select * from testquestions where testid='126'"; 
$result=mysql_query($sql) or die(mysql_error()); 
if (mysql_num_rows($result)>0) 
{ 
$data = array(); // create a variable to hold the information 
while (($row = mysql_fetch_array($result, MYSQL_ASSOC)) !== false) 
{ 
$data[] = $row; // add the row in to the results (data) array 
} 
//shift off the first value 
array_shift($data[0]); 

$merge = call_user_func_array('array_merge', $data); 

$size=sizeof($merge); 
$newdata=array(); 
$num=1; 
echo "<form action='fetchquestion.php' method='post'>"; 

foreach ($merge as $key => $value) 
{ 

$sql2="select qid, question,option1,option2,option3,option4 from question where qid='$value' "; 

    $result2=mysql_query($sql2) or die(mysql_error()); 


    while($row2=mysql_fetch_assoc($result2)) 
      { 
       $questionid=$row2['qid']; 
      $question=$row2['question']; 
      $option1=$row2['option1']; 
      $option2=$row2['option2']; 
      $option3=$row2['option3']; 
      $option4=$row2['option4']; 
       $newdata[]=$row2; 



      echo '<div><div class="question">';  
      echo "$num<h3>$question</h3>"; 

      echo "</div> 
      <div class='answer'> 
      <ul> 
      <li><input type='radio' class='radioBtnClass' name='option' style='width:50px' value='1'/>$option1</li> 
      <li><input type='radio' class='radioBtnClass' name='option' style='width:50px' value='2'/>$option2</li> 
      <li><input type='radio' class='radioBtnClass' name='option' style='width:50px' value='3'/>$option3</li> 
      <li><input type='radio' class='radioBtnClass' name='option' style='width:50px' value='4'/>$option4</li> 
      <input type='hidden' name='count' value='$num' id='count'> 

      <input type='hidden' name='queId' value='$questionid' id='queId'> 
      </ul></div></div>"; 
     $num++; 
      } 



        } 
        echo"<input type='submit' name='submit' value='Submit'><br> 
        </form> 

        ";  
      if (isset($_POST['submit'])) { 

      $_SESSION['quiz'] = array(); 
       // get rid of the submit element in the $_POST 

       unset($_POST['submit']); 
       // assign the values of the radio buttons to the $_SESSION['quiz'] array 
       $_SESSION['quiz'] = $_POST; 
        echo "submitted form"; 
        } 
        else { 
       echo "Please submit the form."; 
         } 



        } 
       else 
       { 
        echo " no questions available for selected test"; 
        } 
        ?>  

我該如何做到這一點? 有人可以幫忙嗎?

+0

它是每頁一個問題?你怎麼知道該選項是爲這個問題:) – 2014-10-20 06:44:27

回答

0
echo "</div> 
      <div class='answer'> 
      <ul> 
      <li><input type='radio' class='radioBtnClass' name='option_$questionid' style='width:50px' value='1'/>$option1</li> 
      <li><input type='radio' class='radioBtnClass' name='option_$questionid' style='width:50px' value='2'/>$option2</li> 
      <li><input type='radio' class='radioBtnClass' name='option_$questionid' style='width:50px' value='3'/>$option3</li> 
      <li><input type='radio' class='radioBtnClass' name='option_$questionid' style='width:50px' value='4'/>$option4</li> 
      <input type='hidden' name='count' value='$num' id='count'> 

      <input type='hidden' name='queId[]' value='$questionid' id='queId'> 
      </ul></div></div>"; 

表格後提交使用此代碼

$data = $_POST; 
    $que_array = $data['queId']; 
    foreach($que_array as $key => $value){ 
     $que_id = $value; 
    $option = 0; 
    if(isset($data['option_' . $que_id])){ 
     $option = $data['option_' . $que_id]; 
    } 

     echo $que_id . "--" . $option. "<br>"; 
    } 
+0

謝謝,我得到的問題id的數組,但沒有得到選定的單選按鈕值。我只得到最後選擇的值。 – 2014-10-20 07:06:19

+0

您是否更改表單代碼

  • $ option1
  • 2014-10-20 07:11:49

    +0

    yes,它的賦值錯誤undefined偏移1,未定義偏移2爲$ key – 2014-10-20 07:19:04

    0

    當你正在使用的陣列單選按鈕的名稱。

    因此,要獲得選擇的無線電使用的值驗證碼:

    echo $_REQUEST['option'][0]; 
    
    相關問題