【发布时间】:2014-07-15 17:25:15
【问题描述】:
我正在尝试使用一个简单的 switch 语句来处理我的表单,但是我的 case 语句被忽略了,除了 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;
}
?>
【问题讨论】:
-
你为什么要设置一个动作?默认操作是自我
-
这是
$_POST,而不是$POST -
好吧,我只是觉得自己像个白痴,但感谢您让我摆脱了困境。我已经更新了上面的代码,现在默认消息没有显示-想法?此外,“重置”按钮根本不起作用,即使我手动刷新浏览器,来自上一个表单条目的消息也会保留在页面上。再次感谢您的帮助!
-
在两个定义语句中加上引号。显然,通知的 php 错误报告没有打开,否则你会注意到它。无论如何它对我有用。
-
对不起-我没有使用这两个定义语句-应该为这个问题删除它们。
标签: php html forms switch-statement