【问题标题】:PHP switch statement for HTML form handling not working as expected用于 HTML 表单处理的 PHP switch 语句未按预期工作
【发布时间】: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


【解决方案1】:

要重定向到文件本身,您可以只使用action="?"(或不执行任何操作)。

那你最好使用long PHP 标签:&lt;?php,而不是&lt;?

如果你在&lt;/html&gt; 文档关闭后输出了一些东西,可能它没有显示出来。确定这不是正确的 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>

更新——我忘记了一件事情。如果答案没有,则$_POST 数组中的“Question1”条目不等于“”——它完全没有。您需要明确测试这种情况,或者如果它不存在则分配一个空字符串。例如:

// 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']) {

最后,如果你的IDE不允许调试,你可以直接打印出$_POST的内容:

default:
    print "<pre>Something strange happened, this is _POST:";
    var_dump($_POST);
    exit();

我推荐一个 IDE,例如带有 PDT 和 XDebug 的 Eclipse。是的,现在这是一个巨大的矫枉过正。 这些文件你可以在记事本中开发,是的。但是给它几个月越来越少的琐碎开发,那我们再来谈谈它:-)

除此之外,它看起来应该可以工作。

修改后的代码

<!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>

【讨论】:

  • Iserni- 这些都没有帮助解决我的问题,但谢谢...我正在使用 Aptana Studio 进行编辑。
  • 你用什么来查看结果?我已将文件上传到服务器,它可以正常工作。
  • ...我也在复制代码的一个版本。这些更改确实是装饰性的,代码从一开始就有效(除了几个警告并且没有识别出缺少的答案)。
  • 这并没有解决我的顾虑,而且远远超出了我的编码知识。我相信在某些时候我会感谢你的 cmets,但现在他们只会让我感到困惑。
  • 对不起。不过,你快到了。我已经修改了我的注释代码,以更好地解释它背后的内容。随时评论、修改或联系我。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-10-05
  • 2020-10-23
  • 1970-01-01
  • 1970-01-01
  • 2020-03-05
  • 2017-10-27
  • 1970-01-01
相关资源
最近更新 更多