2013-06-27 47 views
0

我試圖獲得發佈信息和使用下面的代碼顯示的信息:隱藏屬性從HTML表單

PHP代碼:

 $self = $_SERVER['PHP_SELF']; 
     if(isset($_POST['send'])){     
      $words = htmlspecialchars($_POST['board']); 
      print "<b>".$words."</b>"; 
     }   ​​​​ 

HTML代碼

上述
<form action="<?php $self ?>" method=post> <!--$self is the directory of the page itself--> 
     <p><i>Comment</i></p> 
     <textarea name="board" rows="20" cols="10"></textarea> 
     <input name="send" type="hidden" /> 
     <p><input type='submit' value='send' /></p> 
</form> 

代碼將工作,我intented。但是,如果我擺脫輸入名稱=「發送」的類型=「隱藏」,用戶輸入的消息將不會出現,一旦發送按鈕被點擊。爲什麼會發生?

+2

什麼是你的提交按鈕的名稱? ;) –

+2

您可以刪除'action =「」',因爲您將它指向同一頁面。 –

回答

4

您需要將name ='send'添加到您的提交按鈕,您的PHP代碼正在讀取表單元素的名稱,並且您沒有爲您的提交按鈕指定一個。

<form action="<?php $self ?>" method=post> <!--$self is the directory of the page itself--> 
     <p><i>Comment</i></p> 
     <textarea name="board" rows="20" cols="10"></textarea> 
     <p><input type='submit' name='send' value='send' /></p> 
</form> 

此外,快速的注意 - 你可以改變你的形式方法來獲得,而不是POST很容易地看到你在地址欄發送何種形式的數據。

2

這是因爲要檢查的POST變量「發送」 isset。這就是你命名你的隱藏輸入。

您應該爲您的提交輸入添加一個name。例如:

<p><input type='submit' name="submit_button" value='send' /></p> 

現在,在你的PHP,請檢查您的提交按鈕的name。在這個例子中我使用了「submit_button」。下面是修改後的代碼示例:

$self = $_SERVER['PHP_SELF']; 
    if(isset($_POST['submit_button'])){     
     $words = htmlspecialchars($_POST['board']); 
     print "<b>".$words."</b>"; 
    } 
0

不要費心命名您的發送按鈕或什麼,只是刪除hidden線...

,改變你的PHP ....

$self = $_SERVER['PHP_SELF']; 
    if(isset($_POST)){     
     $words = htmlspecialchars($_POST['board']); 
     print "<b>".$words."</b>"; 
    }