2016-06-11 1 views
1

제가 작업하고있는 Perl CGI 퀴즈가 있습니다.하지만 완전히 갇혀 있습니다. 다음과 같이 내 HTML 코드는 다음과 같습니다Perl CGI - 특이성을위한 라디오 값 선택

<p class="question"><br>1. The answer is Number 1 </p> 
 
<ul class="answers"> 
 
    <input type="radio" name="q1" value="a" id="q1a"><label for="q1a">1912</label><br/> 
 
    <input type="radio" name="q1" value="b" id="q1b"><label for="q1b">1922</label><br/> 
 
    <input type="radio" name="q1" value="c" id="q1c"><label for="q1c">1925</label><br/> 
 
    <input type="radio" name="q1" value="d" id="q1d"><label   for="q1d">Summer of '69</label><br/> 
 
</ul>

CGI 프로그램은 매개 변수 값의 라디오 버튼에서 이름을 따기입니다. 내 Perl 코드는 다음과 같습니다 :

if (param("q1") eq undef) { 
    $an1 = 0; 
    $winner = 0; 
print <<"BIG"; 
     <h1>You didn't enter the right answer</h1> 
BIG 
} 
else { 
print <<"BIG"; 
     <h1>You entered the right answer</h1> 
BIG 
} 

이 시점에서 나는 라디오 박스 중 하나를 선택하면 올바른 대답을 입력했다고합니다.

는 매개 변수 a 또는 b 또는 c 또는 d처럼, 라디오에서 뽑고 가치, 또는 내가 전부 잘못하고 있어요 내가 지정할 수있는 방법이 있습니까?

답변

2

라디오 그룹을 처리하는 방법에 대한 자세한 내용은 CGI documentation을 참조하십시오.

my $cgi = CGI->new; 
my $value = $cgi->param('q1'); 

if ($value eq 'a') { #correct answer 

} 
else { # incorrect answer 
} 

또한, eq는 문자열 비교 연산자이며, undef을 테스트하는 데 사용하지 마십시오 당신에게 예를들합니다. 펄은 defined 함수를 가지고있다.

1

당신 해야 항상use strict모든 펄 프로그램의 상단에 use warnings 'all'. 그 자리에 당신은 메시지를 보았을 것입니다. Use of uninitialized value in string eq

문자열 비교 자 eq을 사용하여 값을 undef과 비교할 수 없습니다. 귀하의 경우 아무런 라디오 버튼을 선택하지 않은 경우 param("q1")a, b, c 또는 d 또는 undef이됩니다. 일반적으로 checked="checked"을 사용하여 이것을 방지하기 위해 기본적으로 선택되는 라디오 버튼 중 하나를 만듭니다.

다음은 기본적인 CGI 프로그램입니다.

use strict; 
use warnings 'all'; 

use CGI::Minimal; 
use File::Spec::Functions 'abs2rel'; 

my $self = abs2rel($0, $ENV{DOCUMENT_ROOT}); 

my $cgi = CGI::Minimal->new; 

my $q1 = $cgi->param('q1') // 'none'; 

my $message = 
     ($q1 eq 'a') ? "<h3>You entered the right answer</h3>" : 
     ($q1 ne 'none') ? "<h3>You didn't enter the right answer</h3>" : 
     ''; 

print <<END; 
Content-Type: text/html 

<html> 
    <head> 
     <title>Test Form</title> 
    </head> 
    <body> 

     <form action="$self"> 

      <p class="question"><br/> 
      1. The answer is Number 1 
      </p> 

      <ul class="answers"> 

       <input type="radio" name="q1" value="none" id="q1a" checked="checked" /> 
       <label for="q1a"><i>Please choose an answer</i></label> 
       <br/> 

       <input type="radio" name="q1" value="a" id="q1a" /> 
       <label for="q1a">1912</label> 
       <br/> 

       <input type="radio" name="q1" value="b" id="q1b" /> 
       <label for="q1b">1922</label> 
       <br/> 

       <input type="radio" name="q1" value="c" id="q1c" /> 
       <label for="q1c">1925</label> 
       <br/> 

       <input type="radio" name="q1" value="d" id="q1d" /> 
       <label for="q1d">Summer of '69</label> 
       <br/> 
      </ul> 

      <input type="submit"> 

     </form> 

     $message 

    </body> 
</html> 

END 
1

당신은 시도해야이

$radio_value = $cgi->param('q1') 

! 당신이 것을 쓰기의 스틸 빨간색 때문에 또한 BIG와 HTML 태그를 닫은 후 끝나지 않는 것 같다 인쇄와 선!

+0

스택 오버플로 구문 형광펜은 Perl에서는 그리 똑똑하지 않습니다. 하지만 당신 말이 맞습니다. HEREDOC 구분 기호 뒤에 빈 줄이 있어야합니다. – simbabque