2012-02-03 2 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> 

그리고 이상한 것은이 스크린 샷에 부착 I`ll : 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

+0

으로 업데이트 할 것입니다. 그렇지 않으면 php 파일은 정상적으로 작동합니다 ...하지만 필요한 것은 생성하지만 ... 처음부터 isset에 대한 내 오류 검사입니다. 나는 체크 박스를 사용했지만 ... 그건 실수 였어. –

관련 문제