2012-02-03 39 views
1

我正在做一個測試HTML/PHP的形式。這裏一切都很好......但單選按鈕卻很奇怪。下面是代碼及以下將是怪異的行爲的截圖:HTML或php單選按鈕的奇怪行爲?

的HTML

<!DOCTYPE html> 
<html> 
    <head> 
    </head> 

    <body> 
     <h1>Differend sided dice</h1> 
     <form action="dice.php" method="post"> 
     <input type="radio" value="4" name="4side" checked />4 side<br /> 
     <input type="radio" value="10"name="10side" />10 side<br /> 
     <input type="radio" value="20"name="20side" />20 side<br /> 
     <input type="submit" > 
     </form> 
    </body> 
</html> 

的PHP

<html><head></head> 
<body> 
<? 

    function rollOut($dice) { 
     $out = <<<HERE 
     <p>You rolled <span style="color:red"> $dice </span> of the diec</p> 
HERE; 
     return $out; 
     } 

    function diceSide() { 
     $dicetype = rand(1, 10); /* default */ 
     if (isset($_POST["4side"])) { 
      $dicetype = rand(1, $_POST["4side"]); 
      print rollOut($dicetype); 
      } 
     else if (isset($_POST["10side"])) { 
      $dicetype = rand(1, $_POST["10side"]); 
      print rollOut($dicetype); 
      } 
     else if (isset($_POST["20side"])) { 
      $dicetype = rand(1, $_POST["20side"]); 
      print rollOut($dicetype); 
      } 
     else { 
     $dicetype = rand(1, 20); 
     rollOut($dicetype); 
     } 
    } 

    /* init main */ 
    diceSide(); 

    ?> 
</body> 
</html> 

而且奇怪的事情我心底重視截圖: bug

所有按鈕的行爲都像複選框?任何信息?

回答

8

你必須爲所有<input type='radio'>設置的同名爲他們預期行動

HTML代碼:

<!DOCTYPE html> 
<html> 
    <head> 
    </head> 

    <body> 
     <h1>Differend sided dice</h1> 
     <form action="dice.php" method="post"> 
     <input type="radio" value="4" name="selectMe" checked />4 side<br /> 
     <input type="radio" value="10"name="selectMe" />10 side<br /> 
     <input type="radio" value="20"name="selectMe" />20 side<br /> 
     <input type="submit" > 
     </form> 
    </body> 
</html> 

PHP代碼(我把它簡化的自由一點--should做與之前相同的東西,只是更少的代碼):

<html><head></head> 
<body> 
<? 

    function rollOut($dice) { 
     $out = '<p>You rolled <span style="color:red"> '.$dice .'</span> of the diec</p>'; 
     return $out; 
     } 

    function diceSide() { 
     $dicetype = rand(1, 10); /* default */ 

     if (isset($_POST["selectMe"])) { 
      $dicetype = rand(1, $_POST["selectMe"]); 
      } 
     print rollOut($dicetype); 
    } 
    /* init main */ 
    diceSide(); 

    ?> 
</body> 
</html> 
+0

你是說提交類型破壞代碼?我應該使用

+0

我會更新一個代碼示例 – Bogdan 2012-02-03 16:03:49

+0

,否則PHP文件就好了...生成我需要的但是...我雖然從一開始就是我對isset的錯誤檢查,我試過了!我在複選框上做了,但...猜猜它是錯誤的HTML。 – 2012-02-03 16:05:53