2013-02-19 22 views
0

我有以下的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";調用。

我究竟做錯了什麼?

+0

使用'if($ _SERVER ['REQUEST_METHOD'])=='POST')'代替。它是100%可靠的,不像(如你的案例所示)檢查特定的表單域。 – 2013-02-19 15:37:45

回答

5

您的提交按鈕沒有名稱屬性,所以它不會是一個成功的控件,並且不會出現在提交的數據中(當然不會顯示爲$_POST["submit"])。

1

給出一個name="submit"submit表單輸入。

0

檢查您的php_error.log文件。它應該說一些關於你如何試圖訪問一個未定義的索引「提交」。

問題是沒有提交鍵,因爲你沒有給出提交輸入「名稱」值。

相關問題