用于HTML表单处理的PHP开关语句未按预期工作
我试图用一个简单的switch语句来处理我的表单,但是我的case语句被忽略了,除了在Chrome中出现并显示的默认消息之外,不管表单输入是什么。在Firefox中,当我点击submit时,它会进入层次结构中我的工作文件夹上方的根文件夹。谢谢你的帮助用于HTML表单处理的PHP开关语句未按预期工作,php,html,forms,switch-statement,Php,Html,Forms,Switch Statement,我试图用一个简单的switch语句来处理我的表单,但是我的case语句被忽略了,除了在Chrome中出现并显示的默认消息之外,不管表单输入是什么。在Firefox中,当我点击submit时,它会进入层次结构中我的工作文件夹上方的根文件夹。谢谢你的帮助 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org /TR/xhtml1/DTD/xhtml1-transitional.dt
<!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 Switch Form</title>
</head>
<body>
<form name="GetForm" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="POST">
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>
<?
switch ($_POST['Question1']) {
case 'a':
echo "You are correct.";
break;
case 'b':
echo "Your answer is incorrect";
break;
case 'c':
echo "Your answer is incorrect";
break;
case '':
echo "Please enter a response to the question.";
break;
default:
echo "Welcome to my simplest of php SWITCH scripts";
break;
}
?>
一种简单的开关形式
要重定向到文件本身,只需使用action=“?”
(或不执行任何操作)
然后最好使用长PHP标记:为什么要设置动作?默认操作是selfIt's$\u POST
,而不是$POST
,我只是觉得自己像个白痴,但谢谢你让我克服了这一点。我已经更新了上面的代码,现在默认消息不显示-ideas?此外,“重置”按钮根本不起作用,即使我手动刷新浏览器,上一个表单条目中的消息也会保留在页面上。再次感谢你的帮助!在两个define语句中加引号。显然,通知的php错误报告没有打开,否则您会注意到它。无论如何,这对我来说是有效的。抱歉-我没有使用这两个定义语句-应该在这个问题上删除它们。Iserni-这些都没有帮助我解决问题,但谢谢。。。我正在使用Aptana Studio进行编辑。您使用什么查看结果?我已经把文件上传到服务器上,它正常工作了……我还复制了一个版本的代码。这些改变真的是表面上的,代码从一开始就工作了(除了一些警告和不识别缺失的答案)。这并没有解决我的问题,远远超出了我的编码知识。我相信在某个时候我会很感激你的评论,但现在它们只会让我感到困惑。我很抱歉。不过,你就快到了。我已经修改了我的注释代码,以便更好地解释它背后的含义。请随时评论、修改或与我联系。
define('ERROR', ...
</form>
<?php
define('ERROR', 'No answer was input for this question. Please select and answer.');
define('GREETING', 'Welcome to my simplest of php forms.');
if (!array_key_exists('Question1', $_POST)) {
exit(); // We're just displaying the form.
}
switch ($_POST['Question1']) {
...
}
?>
</body>
</html>
// Create an "answer" array from _POST. Missing entries will be added with
// a default value.
$answer = array_merge(array(
'Question1' => '',
'Question2' => '',
), $_POST);
switch($answer['Question1']) {
default:
print "<pre>Something strange happened, this is _POST:";
var_dump($_POST);
exit();
<!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><title>A Simple Switch Form</title></head>
<body>
<form name="GetForm" method="POST">
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" name="cmd" value="GO" /><input type="reset" value="RESET" />
</form>
<?php
define('ERROR', 'No answer was input for this question. Please select and answer.');
define('GREETING', 'Welcome to my simplest of php forms.');
/*
The $_POST array always exists. If this page is loaded standalone, it will be
empty. If it is loaded as reaction to a question being answered, we need a way
to tell. It can't be the question itself, because if it was left blank, then
we could not tell between the "standalone" and "empty answer" cases.
So we add a field that we're sure will always be transmitted, linked to the
submit button, by naming this button "cmd".
Now, the presence of "cmd" in $_POST will tell us that a question has been
answered (or attempted). Its absence will tell that we just need to show the
question for the first time.
*/
if (!array_key_exists('cmd', $_POST)) {
// Just showing the form.
echo GREETING;
} else {
// "cmd" is present, so we know that a question has been attempted.
// Has it been answered? Now we need to check for this.
if (!array_key_exists('Question1', $_POST)) {
// Did not answer.
// We replace "no answer" (Question1 not found) with "empty answer"
// (Question1 equal to '') so that we can treat "no-answer" as any other answer.
$_POST['Question1'] = '';
}
switch ($_POST['Question1']) {
case 'a':
echo "You are correct.";
break;
// b and c elicit the same reaction, so the two switch cases can be
// written together.
case 'b':
case 'c':
echo "Your answer '{$_POST['Question1']}' is incorrect";
break;
case '':
echo ERROR;
break;
default:
// We should not be here, because from the form one can only send
// a, b or c, and if he does none, we receive the empty string.
// However, it is good policy to foresee a "default" case and intercept
// possible unforeseen circumstances.
echo GREETING;
break;
}
}
?>
</body>
</html>