2014-05-17 48 views
0

我一直在研究這個簡單的「獲取」表格一個多星期,並且正在給叔叔打電話。我希望表單在用戶點擊「提交」之前回顯GREETING常量,即頁面首次加載時。即使沒有點擊'提交','沒有用戶輸入'的響應加載並忽略了GREETING。這是我的代碼w/out if(isset($ _ GET [「submit」]))。

當我添加if(isset ..)GREETING常量加載但所有其他操作都被忽略。

代碼瓦特/ isset:

<!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. ""; } 

?> 

這裏是與if(isset代碼..

<!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,這意味着當您加載首次頁,它通過GET的,和

$answer1=$_GET['Question1'] 

將得到執行,使$answer1null,因爲您的網址中沒有Question1參數。

那麼這來了

elseif ($answer1=="") 

和計算結果爲真實的,因爲一個標準的==平等的測試,null == ""是TRUE。所以繁榮,你輸出一個虛假的錯誤信息。

你可能避免這種情況,可能與

elseif ($answer1 === "") 

注意=的跡象,這意味着這是一個嚴格的平等的測試:數據類型和值必須匹配。

也就是說或開關的形式來使用POST提交,然後就可以圍出整個形式處理部與

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

由於當我改變形式的方法爲「POST」和添加的($ _SERVER [ 'REQUEST_METHOD'] =='POST')它工作得非常好。我無法使用您的建議獲取$ _GET表單方法。那謝謝啦! –

相關問題