2011-04-12 64 views
0
$host = 'localhost'; 
$user = 'root'; 
$pw = ''; 
$db = 'pmdb'; 

mysql_connect($host,$user,$pw); 
mysql_select_db($db); 

$result = mysql_query("SELECT * FROM Questions WHERE QuizID=1"); 
$num_rows = mysql_num_rows($result); 
while($row = mysql_fetch_assoc($result)) 
{ 
    $array[] = $row; 
} 
for($i=0; $i<=($num_rows-1); $i++) 
{ 
    $title = $array[$i]['Title']; 
    $ans1 = $array[$i]['Answer1']; 
    $ans2 = $array[$i]['Answer2']; 
    $ans3 = $array[$i]['Answer3']; 
    $ans4 = $array[$i]['Answer4']; 
    echo $title.'<br>'; 
    echo '<form method="post">'; 
    echo '<input type="radio" name="ans'.$i.'">'.$ans1.'<br>'; 
    echo '<input type="radio" name="ans'.$i.'">'.$ans2.'<br>'; 
    echo '<input type="radio" name="ans'.$i.'">'.$ans3.'<br>'; 
    echo '<input type="radio" name="ans'.$i.'">'.$ans4.'<br>'; 
} 
echo '<input type="submit" value="submit" id="submit">'; 
echo '</form>'; 

我設法顯示問題後跟相應的選項,並在底部提交按鈕。幫助檢索單選按鈕的答案

如果點擊提交按鈕,我將如何獲取用戶爲從數據庫中循環出來的每個問題選擇的值? ans1,ans2

這是必要的比較他們的答案和答案的關鍵,並計算他們的分數。

。請幫忙!非常感謝你和更多的權力!

+0

你輸出每個問題的'

'標籤,這使得HTML無效。這應該在父'for'循環之外完成。 – 2011-04-12 17:39:49

+0

。好的,我已經糾正它..接下來應該是什麼? – zerey 2011-04-12 17:42:23

+0

您正在關閉循環之外的'' ...您必須在'}'內部關閉您的''並對其應用操作..'

'。 ..或者移出循環的第一個表單標籤'' – pufos 2011-04-12 17:51:15

回答

1

值分配給每個無線電投入,更新的腳本可以是:

$host = 'localhost'; 
$user = 'root'; 
$pw = ''; 
$db = 'pmdb'; 

mysql_connect($host,$user,$pw); 
mysql_select_db($db); 

$result = mysql_query("SELECT * FROM Questions WHERE QuizID=1"); 
$num_rows = mysql_num_rows($result); 
while($row = mysql_fetch_assoc($result)) 
{ 
    $array[] = $row; 
} 

//Start the form 
echo "<form method=\"post\" action=\"path/to/receiving.php\">\n"; 

for($i=0; $i<=($num_rows-1); $i++) 
{ 
    //Render a question + the answer choices 
    echo $array[$i]['Title']."<br />\n"; 
    for ($j=1;$j<=4;$j++) { 
    echo "<input type=\"radio\" name=\"ans$i\" value=\"$j\">". 
     $array[$i]['Answer'.$j]."<br />\n"; 
    } 
} 

//End the form 
echo "<input type=\"submit\" value=\"submit\" id=\"submit\">\n</form>"; 

我們瞭解PHP內讀取的答案值:

echo $_POST["ans1"]; 
    //The answer given for question 1, will be between 
    // 1-4 or null (if they didn't answer 
+0

。然後我接下來要做什麼? – zerey 2011-04-12 17:40:04

+0

@zerey:在線查找使用PHP處理'$ _POST'數據的基本教程。 – drudge 2011-04-12 17:41:04