即使使用if(isset($_GET["submit"]))), php也会在点击'提交'之前加载;


php loads before hitting ‘submit’ even when using if(isset($_GET["submit"])); ignores constant ‘GREETING’

我已经花了一个多星期的时间在这个简单的' get '表单上了。我希望表单在用户点击"提交"之前回声GREETING常量-即当页面首次加载时。即使没有点击' submit ', ' no user input '的响应也会加载并忽略GREETING。这是我的代码w/out if(isset($_GET[" submit "]))

当我添加if(isset..)时,GREETING常量加载,但所有其他动作都被忽略。

代码w/out 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. "";  }
?>

Simple:您正在使用GET,这意味着当您第一次加载页面时,它是通过GET,并且

$answer1=$_GET['Question1']

将被执行,使$answer1成为null,因为URL中没有Question1参数。

然后出现

elseif ($answer1=="")

且求值为true,因为使用标准==相等性检验,null == ""为true。因此,您输出了一个虚假的错误消息。

您可以使用

避免这种情况。
elseif ($answer1 === "")

注意三个 =符号,这意味着这是一个严格的相等性测试:数据类型和值必须匹配。

或者将表单切换到使用POST提交,然后您可以使用

隔离整个表单处理部分。
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
   ... process form here
}