2014-05-17 2 views
0

저는이 간단한 'get'양식을 일주일 넘게 일하고 삼촌에게 전화하고 있습니다. 사용자가 '제출'을 누르기 전에 양식이 GREETING 상수를 반향시키기를 원합니다. 즉, 페이지가 처음로드 될 때입니다. 'submit'을 치지 않고도 'no user input'에 대한 응답은로드되고 GREETING은 무시됩니다. 다음은 if (isset ($ _GET [ "submit"]))를 사용하는 코드입니다.

if (isset ..)를 추가하면 GREETING 상수가로드되지만 다른 모든 작업은 무시됩니다.

코드/축소는 isset W : 여기

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>A Simple Get Form</title> 
</head> 
<body> 

<form name="GetForm" action="<?php echo htmlentities($_SERVER['PHP_SELF']);?>" method="GET"> 
1) What are the colors of the U.S. Flag?<br> 
<input type="radio" name="Question1" value="a" />a) red, white and blue<br /> 
<input type="radio" name="Question1" value="b" />b) yellow, red and blue<br /> 
<input type="radio" name="Question1" value="c" />c) blue, green and amber <br> 
<input type="submit" value="GO" /><input type="reset" value="RESET" /> 
</form> 
</body> 
</html> 

<? 

define(ERROR, 'No answer was input for this question. Please select and answer.'); 
define(GREETING, 'Welcome to my simplest of php forms.'); 
$answer1=$_GET['Question1']; 

if($answer1=="a") 
{echo "You are correct."; } 

elseif ($answer1=="") 
{echo ERROR. "" ; } 

elseif ($answer1=="b" || $answer1=="c") 
{echo "Your answer was incorrect."; } 

else {echo GREETING. ""; } 

?> 

는 경우 (는 isset WITH 코드입니다 .. 간단한

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>A Simple Get Form</title> 
</head> 
<body> 

<form name="GetForm" action="<?php echo htmlentities($_SERVER['PHP_SELF']);?>" method="GET"> 
1) What are the colors of the U.S. Flag?<br> 
<input type="radio" name="Question1" value="a" />a) red, white and blue<br /> 
<input type="radio" name="Question1" value="b" />b) yellow, red and blue<br /> 
<input type="radio" name="Question1" value="c" />c) blue, green and amber <br> 
<input type="submit" value="GO" /><input type="reset" value="RESET" /> 
</form> 
</body> 
</html> 
<? 

define(ERROR, 'No answer was input for this question. Please select and answer.'); 
define(GREETING, 'Welcome to my simplest of php forms.'); 
$answer1=$_GET['Question1']; 

if(isset($_GET["submit"])) 

{ 
if($answer1=="a") 
{echo "You are correct."; } 

elseif ($answer1=="") 
{echo ERROR. "" ; } 

elseif ($answer1=="b" || $answer1=="c") 
{echo "Your answer was incorrect."; } 
} 
else {echo GREETING. ""; } 

?> 

답변

0

: 당신은 당신이를로드 할 때 즉, GET을 사용하고 처음 페이지될 $answer1을, 그것은 GET을 통해, 그리고

$answer1=$_GET['Question1'] 

이 실행 얻을 것이다URL에 Question1 매개 변수가 없기 때문입니다.

그런 다음이 null == ""이 TRUE, 최대

elseif ($answer1=="") 

을 제공하고 있기 때문에 표준 == 평등 테스트로, true로 평가합니다. 그래서 붐, 당신은 가짜 오류 메시지를 출력합니다.

는이 엄격한 평등 테스트입니다 의미

elseif ($answer1 === "") 

참고로, 가능성의 = 흔적이를 피할 수 : 데이터 유형과 값이 일치해야합니다.

그 또는 제출 POST를 사용하여 양식을 전환 한 다음 당신은

if ($_SERVER['REQUEST_METHOD'] == 'POST') { 
    ... process form here 
} 
+0

감사와 전체 형태 처리부 오프 울타리 나는 양식 방법 'POST'를 변경하고 ($ _SERVER를 [추가 할 수 있습니다 'REQUEST_METHOD'] == 'POST') 그것은 굉장히 효과가있었습니다. 나는 당신의 제안을 사용하여 전혀 작동하지 않는 $ _GET 양식 메소드를 얻을 수 없었다. 감사합니다! –

관련 문제