Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/240.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php在点击‘之前加载;提交’;即使使用if(isset($u GET[“submit”]);忽略常数‘;问候’;_Php_Forms_If Statement_Isset - Fatal编程技术网

php在点击‘之前加载;提交’;即使使用if(isset($u GET[“submit”]);忽略常数‘;问候’;

php在点击‘之前加载;提交’;即使使用if(isset($u GET[“submit”]);忽略常数‘;问候’;,php,forms,if-statement,isset,Php,Forms,If Statement,Isset,我已经写了一个多星期这个简单的“get”表格了,我正在给叔叔打电话。我希望表单在用户点击“提交”之前(即页面首次加载时)回显问候语常量。即使不点击“提交”,也会加载“无用户输入”的响应并忽略问候语。这是我的代码,没有if(isset($\u GET[“submit”]) 当我添加if(isset..)时,问候语会不断加载,但所有其他动作都会被忽略 代码w/out isset: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional

我已经写了一个多星期这个简单的“get”表格了,我正在给叔叔打电话。我希望表单在用户点击“提交”之前(即页面首次加载时)回显问候语常量。即使不点击“提交”,也会加载“无用户输入”的响应并忽略问候语。这是我的代码,没有if(isset($\u GET[“submit”])

当我添加if(isset..)时,问候语会不断加载,但所有其他动作都会被忽略

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

?>

一个简单的Get表单

简单:您使用的是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
}

感谢我将表单方法更改为“POST”并添加($\u服务器['REQUEST\u method']=='POST'),它工作得非常出色。使用您的建议,我根本无法使$\u get form方法工作。谢谢!
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
   ... process form here
}