我试图得到一个简单的开关语句与我的表单工作,但我的情况下的语句被忽略,除了默认的消息,在Chrome出现并显示无论什么表单输入。在Firefox中,当我点击提交时,它会转到层次结构中工作文件夹上方的根文件夹。谢谢你的帮助。
<!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标签:<?php
,而不是<?
。
如果您在</html>
文档关闭后输出一些东西,它可能是它不显示。这肯定不是正确的HTML。
常量应该加引号:
define('ERROR', ...
因为代码总是执行,你必须确保只检查$_POST
,如果它是合适的:
</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>
UPDATE——我忘记了一件事。如果没有答案,则$_POST
数组中的'Question1'条目不等于"——它完全缺少。您需要显式地测试这种情况,或者在不存在的情况下分配一个空字符串。例如:
// Create an "answerswer" array from _POST. Missing entries will be added with
// a default value.
$answer = array_merge(array(
'Question1' => '',
'Question2' => '',
), $_POST);
switch($answer['Question1']) {
最后,如果您的IDE不允许调试,您可以直接打印出$_POST
的内容:
default:
print "<pre>Something strange happened, this is _POST:";
var_dump($_POST);
exit();
我推荐像Eclipse/PDT和XDebug这样的IDE。是的,现在这是一个巨大的过度杀伤。这些文件可以在记事本中开发。但是给它几个月越来越少的琐碎的开发,然后我们再讨论它:-)
除此之外,它看起来应该可以工作。
<标题>改变代码<!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>
标题>