为什么我的PHP没有检测到我的提交按钮按下,因此没有执行我的功能


Why is my PHP not detecting my submit button press and thus not executing my function?

我在index.php页面的中间有以下HTML:

<form action="" method="post"><input type="submit" value="Reset Game" class="reset-button" title="Admitting defeat?"></form>

在我的页面顶部,我有以下检查:

if (isset($_POST["submit"]) {
    if ($_POST["submit"] === "Reset Game"] {
        $_SESSION["word"] = generate_word("dictionary.txt");
        $_SESSION["word-progress"] = turn_to_underscores($_SESSION["word"]);
    }
}

它生成了我的刽子手游戏的单词,以及下划线版本。以下是这些功能:

    /**
     * Generate a random word from the given dictionary file
     * @param $filename Name of the dictionary file
     * @return Random word
     */
    function generate_word($filename) {
        $dictionary_file = new SplFileObject($filename);
        $dictionary_file->seek(rand(0, 80367));
        return trim($dictionary_file->current());
    }
    /**
     * Accepts a word and returns it as underscores for obfuscation
     * @param $word A given word
     * @return Returns the word as underscores
     */
    function turn_to_underscores($word) {
        $underscored_word = "";
        for ($i = 0; $i < strlen($word); $i++) {
            $underscored_word .= "_";
        }
        return $underscored_word;
    }

我用require "functions.php";电话带来的。

我到底做错了什么?

您的提交按钮没有name属性,因此它不会是一个成功的控件,也不会出现在提交的数据中(当然不会显示为$_POST["submit"])。

submit表单输入一个name="submit"

检查php_error.log文件。它应该说明你是如何试图访问一个未定义的索引"提交"的。

问题是没有提交键,因为您没有给提交输入一个"name"值。