我有以下的HTML類似一半,我的index.php頁面:爲什麼我的PHP沒有檢測到我的提交按鈕按下,因此不執行我的功能?
<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";
調用。
我究竟做錯了什麼?
使用'if($ _SERVER ['REQUEST_METHOD'])=='POST')'代替。它是100%可靠的,不像(如你的案例所示)檢查特定的表單域。 – 2013-02-19 15:37:45