2013-11-05 65 views
-1

我想要使用php獲取所選單選按鈕的值。但是,當我嘗試回顯收音機的值時,它會返回'開',無論我提交表單時選擇哪個單選按鈕。在PHP返回的電臺選擇不是值

HTML:

<article> 
    <form id="quiz" method="post" action="question3.php"> 
     <fieldset id="question"> 
      <legend>Question 2</legend> 
      <p>Where was CoffeeScripts first stable build announced? <br /> 
      <label for="github">GitHub</label> 
      <input type="radio" name="ans2" id="github" value="GitHub"/> <br /> 
      <label for="hackernews">Hacker News</label> 
      <input type="radio" name="ans2" id="hackernews" value="Hacker News"/> <br /> 
      <label for="coffeescript">CoffeeScript.com</label> 
      <input type="radio" name="ans2" id="coffeescript" value="CoffeeScript"/> <br /> 
      <label for="dropbox">DropBox</label> 
      <input type="radio" name="ans2" id="dropbox" value="Dropbox"/> <br /> 
     </fieldset> 
     <p><input type="submit" value="Submit Answer" id="subans" /></p> 
    </form> 
</article> 

然後進程頁上:

<?php 

if (isset($_POST['ans2'])) { 
    $ans = $_POST['ans2']; 
    echo "<p>$ans</p>"; 
} 
else { 
    echo "Nothing was selected."; 
} 

?> 

就像我說的,這只是輸出 「關於」 頁面。

幫助感謝!

+0

請分享您的代碼的形式部分。 –

+5

您可能有另一個使用名稱「ans2」的元素來覆蓋值。 –

+0

顯示'

'內的所有內容' –

回答

0

你可以試試這個,添加isset($ _ POST [ 'subans'])在PHP塊

 <article> 
      <form id="quiz" method="post" action="question3.php"> 
       <fieldset id="question"> 
        <legend>Question 2</legend> 
        <p>Where was CoffeeScripts first stable build announced? <br /> 
        <label for="github">GitHub</label> 
        <input type="radio" name="ans2" id="github" value="GitHub"/> <br /> 
        <label for="hackernews">Hacker News</label> 
        <input type="radio" name="ans2" id="hackernews" value="Hacker News"/> <br /> 
        <label for="coffeescript">CoffeeScript.com</label> 
        <input type="radio" name="ans2" id="coffeescript" value="CoffeeScript"/> <br /> 
        <label for="dropbox">DropBox</label> 
        <input type="radio" name="ans2" id="dropbox" value="Dropbox"/> <br /> 
       </fieldset> 
       <p><input type="submit" value="Submit Answer" id="subans" name="subans" /></p> 
      </form> 
     </article> 

在PHP頁面/條,增加了isset($ _ POST [ 'subans'])。

 <?php 

      if(isset($_POST['subans'])){ // added the submit button action check 

        if (isset($_POST['ans2'])) { 
         $ans = $_POST['ans2']; 
         echo "<p>$ans</p>"; 
        } 
        else { 
         echo "Nothing was selected."; 
        } 
      } 

     ?> 
+0

這工作!謝謝。 – user2954911