2014-02-18 56 views
0

我已經通過了很多關於PHP和單選按鈕的問題,但我還沒有找到幫助我的情況。我正在爲服務器端腳本類構建測驗。我被困在實際的測驗部分。如果用戶不選擇問題的答案,我想要一個錯誤消息來回顯。相反,當我點擊提交按鈕而沒有選擇任何選項時,我會自動重定向到結果頁面。如何糾正我的代碼,以便用戶如果在未回答所有問題的情況下提交提交就會收到錯誤消息?以下是我寫出的當前代碼,試圖檢查單選按鈕和後續步驟。所有的返回錯誤,如果單選按鈕沒有被選中 - PHP

<div id="question5"> <!-- each quiz question gets this set up --> 
<h2 id="question5_error">WAIT! You did not pick an answer to this question!</h2> 
<p>5. What creature pulls the Hogwarts carriages?<br> 
<input type="radio" name="creature" value="Cornish Pixies" <?php if (isset($_SESSION["creature"]) && $_SESSION["creature"]="Cornish Pixies"){ echo"checked";} ?> > Cornish Pixies<br> <!-- php code checks to see if radio button has been selected--> 
<input type="radio" name="creature" value="Clydesdales" <?php if (isset($_SESSION["creature"]) && $_SESSION["creature"]="Clydesdales"){ echo"checked";} ?> > Clydesdales<br> 
<input type="radio" name="creature" value="Hippogriffs" <?php if (isset($_SESSION["creature"]) && $_SESSION["creature"]="Hippogriffs"){ echo"checked";} ?> > Hippogriffs<br> 
<input type="radio" name="creature" value="Thestrals" <?php if (isset($_SESSION["creature"]) && $_SESSION["creature"]="Thestrals"){ echo"checked";} ?> > Thestrals<br> 

<input type="hidden" name="sent" value="sent" onclick="Project1_Page2.php"> 
<input type="submit" name="goresults" value="Get Results?">|<input type="reset" name="resetquiz" value="Reset Quiz."> 
</form> 
</article> 
<?php 
if(isset($_GET["sent"])){ 
echo "<script type = 'javascript'>"; 
if(!isset($_GET["question1"])){ // QUIZ QUESTION 1 CHECK 
     $_SESSION["author"]=$_GET["author"]; 
    } 
    else{ 
     echo "document.getElementById('question1').style.backgroundColor = '#ffcccc'; 
     document.getElementById('question1_error').style.display = 'block';"; 
    } 
if(!isset($_GET["question2"])){ // QUIZ QUESTION 2 CHECK 
    $_SESSION["godfather"]=$_GET["godfather"]; 
    } 
    else{ 
     echo"document.getElementById('question2').style.backgroundColor = '#ffcccc'; 
     document.getElementById('question2_error').style.display = 'block';"; 
    } 
if(!isset($_GET["question3"])){ // QUIZ QUESTION 3 CHECK 
    $_SESSION["prison"]=$_GET["prison"]; 
    } 
    else 
    { 
     echo"document.getElementById('question3').style.backgroundColor = '#ffcccc'; 
     document.getElementById('question3_error').style.display = 'block';"; 
    } 
if(!isset($_GET["question4"])){ // QUIZ QUESTION 4 CHECK 
    $_SESSION["badguy"]=$_GET["badguy"]; 
    } 
    else 
    { 
     echo"document.getElementById('question4').style.backgroundColor = '#ffcccc'; 
     document.getElementById('question4_error').style.display = 'block';"; 
    } 
if(!isset($_GET["question5"])){ // QUIZ QUESTION 5 CHECK 
    $_SESSION["creature"]=$_GET["creature"]; 
    } 
    else 
    { 
     echo"document.getElementById('question5').style.backgroundColor = '#ffcccc'; 
     document.getElementById('question5_error').style.display = 'block';"; 
    } 
echo "</script>"; 
} 
?> 
+0

您是否研究過標準表單驗證?可以在客戶端進行檢查(除了服務器端檢查)。 – admdrew

+0

我沒有超出我們在課堂上討論過的內容 - 這是非常基本的,並沒有處理單選按鈕。我會仔細研究它現在的想法。 –

+0

'session_start();'(隱藏)在那裏?利用'空' –

回答

0

首先,如果你使用的是你已經設置的屬性名稱一個無線電像

<form method="post"> 
    <input name="question" value="1" /> 
    <input name="question" value="2" /> 
    <input name="question" value="3" /> 
    <input name="question" value="4" /> 
</form> 
這樣

你設定邏輯的語句,像這樣。

 


    if(isset($_POST['question'])){ 

    //here you put the the right answer based on the user what he/she chose. 


    } 
 

我建議你使用POST是安全的。如果您正在使用GET,收音機的值將出現在網址上。我希望這將有所幫助。

和平

+0

好吧,讓我把這個添加到我的代碼,看看會發生什麼。 –

+0

如果沒有顯示錯誤,您也可以添加else。 –

+0

我確實添加了一個if(!isset)來顯示錯誤。它不填充,但現在我得到每個問題的'未定義索引'錯誤。 –

0

沒有看到你的HTML,這很難說,但這裏有一些建議

在你的表單標籤

,刪除 「action」 屬性

<form method="post/get" > 

然後創建一個變量

$haserror = false; 

我不確定你想在這裏實現什麼,但cha這樣的代碼

if(!isset($_GET["question1"])){ // QUIZ QUESTION 1 CHECK 
     $_SESSION["author"]=$_GET["author"]; 
     echo "document.getElementById('question1').style.backgroundColor = '#ffcccc'; 
     document.getElementById('question1_error').style.display = 'block';"; 

$ haserror = true; }

做同樣爲所有複選框

然後做你的,如果顯示結果頁面

if(!$haserror) 
{ 
    echo '<META http-equiv="refresh" content="0;URL=newpage.php">'; 

} 

不過我建議重寫了整個事情,因爲你是在接近它錯誤的方式

+0

我現在就試試這個。 –

+0

至於重寫....目前的格式是我們在課堂上使用過的一個樣本,所以我不知道我會從哪裏開始。 –

相關問題