@Lord Pacal: the attributes 'name' and 'id' have different intent and are used differently. 'id' uniquely identifies an element in the HTML DOM, whereas 'name' identifies the field that will be transmitted on the form submit. You can omit the 'id' attribute, but not the 'name' attribute.
Where a field requires multiple elements, e.g. radio buttons and check boxes, the 'name' attribute must be the same but the 'id' must be unique for each element.
'id' is useful for linking explicitly to the element, as in the following cases:
* CSS selector by ID
* JavaScript code, e.g. in-browser form validation or active forms
* the target of a <label> element
The last example is one not used nearly enough, as it allows the user to click on the text next to a radio button or check box - a bigger target and thus easier for users not used to using a mouse:
<input type="checkbox" name="example" value="yes" id="example_yes"/>
<label for="example_yes">Yes, I can click on the text and check the box</label>
處理表單
PHP 最強大的功能之一是它對 HTML 表單的處理能力。HTML 表單所傳回的所有項目將自動的提供給您的程式使用。更多使用表單的範例可參考來自 PHP 以外的變數一章。下面是 HTML 表單的範例:
Example#1 一個簡單的 HTML 表單
<form action="action.php" method="post"> <p>Your name: <input type="text" name="name" /></p> <p>Your age: <input type="text" name="age" /></p> <p><input type="submit" /></p> </form>
這只是一個普通的 HTML 表單。它並不包含任何特別的標籤。當使用者填好表格,按下提交鍵後,表單中的所有資料將傳給 action.php,而此檔案則有下列的內容:
Example#2 輸出表單的內容
Hi <?php echo htmlspecialchars($_POST['name']); ?>.
You are <?php echo (int)$_POST['age']; ?> years old.
此腳本的輸出樣本為:
Hi Joe. You are 22 years old.
除了 htmlspecialchars() 和 (int)的部分之外,應該很明白這個程式做了什麼。htmlspecialchars()確定 html 中特殊字元被編碼,所以其他人無法在您的網頁放入 HTML 標籤或是 Javascript。關於 age 那一欄,我們知道它是一個數值,所以可以用convert來去除任何不正確的字元。也可以使用filter擴充功能讓 PHP 自動做這些事。PHP 自動的為您設定了 $_POST["name"] 和 $_POST["age"] 兩個變數。之前,我們使用過 $_SERVER 這個 superglobal,而上述例子則為您介紹了含有所有 POST 資料的 $_POST autoglobal。請留意我們表單所使用的方法(method)是 POST。如果我們剛才使用的是 GET,那所有的表單資料將存在 $_GET 這個 superglobal 裡了。若您不想理會表單使用了哪一種方法,那您可以改為使用 $_REQUEST 這個 autoglobal。它包含了所有 GET、POST、COOKIE 和 FILE 的資料。請參考 import_request_variables() 函式。
也可以在 PHP 中處理 XForms 的輸入,儘管可能更喜歡使用長久以來支援良好的 HTML 表單。XForms 目前還不適合初學者使用,但是您可能對它感興趣。手冊中在「特點」有章節對如何處理從 XForum 接收到的資料進行了簡短的介紹。
處理表單
11-Aug-2008 08:55
30-Mar-2008 01:27
I was so shocked that some servers have a problem regarding the Submit Type Name and gives a "Not Acceptable error" or Error 406.
Consider the example below :
<form action="blah.php" method="POST">
<table>
<tr>
<td>Name:</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" name="Submit_btn" id="Submit_btn" value="Send">
</td>
</tr>
</table>
</form>
This very simple code triggers the "Not Acceptable Error" on
PHP Version 5.2.5 and Apache 1.3.41 (Unix) Server.
However to fix this below is the right code:
<form action="blah.php" method="POST">
<table>
<tr>
<td>Name:</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" name="Submitbtn" id="Submit_btn" value="Send">
</td>
</tr>
</table>
</form>
The only problem that took me hours to find out is the "_" in the Submit Button.
Hope this help!
04-Jan-2008 10:38
One thing that tripped me up when I was first learning PHP was the use of the NAME attribute in form fields. The current convention is to use the ID attribute instead when creating forms. (Many HTML editors automatically include an ID attribute without a NAME attribute.) Now, I include both the NAME and ID attributes (with the same value) in all my form fields.
For example...
<form method="post">
<input type="text" id="field1">
<input type="submit" value="go">
</form>
If you then have a PHP page requesting the contents of the "field1" field...
<?php echo $_POST["field1"] ?>
...the above form will always return an empty string for "field1".
The solution is to include the NAME attribute...
<form method="post">
<input type="text" id="field1" name="field1">
<input type="submit" value="go">
</form>
With this change, the PHP code will correctly retrieve the value of the "field1" field.
09-Nov-2006 12:02
As Seth mentions, when a user clicks reload or goes back with the browser button, data sent to the server, may be sent again (after a click on the ok button).
It might be wise, to let the server handle whatever there is to handle, and then redirect (a redirect is not visible in the history and thus not reachable via reload or "back".
It cannot be used in this exact example, but as Seth also mentions, this example should be using GET instead of POST
05-May-2005 03:18
[Editor's Note: Since "." is not legal variable name PHP will translate the dot to underscore, i.e. "name.x" will become "name_x"]
Be careful, when using and processing forms which contains
<input type="image">
tag. Do not use in your scripts this elements attributes `name` and `value`, because MSIE and Opera do not send them to server.
Both are sending `name.x` and `name.y` coordiante variables to a server, so better use them.
02-Dec-2003 04:55
According to the HTTP specification, you should use the POST method when you're using the form to change the state of something on the server end. For example, if a page has a form to allow users to add their own comments, like this page here, the form should use POST. If you click "Reload" or "Refresh" on a page that you reached through a POST, it's almost always an error -- you shouldn't be posting the same comment twice -- which is why these pages aren't bookmarked or cached.
You should use the GET method when your form is, well, getting something off the server and not actually changing anything. For example, the form for a search engine should use GET, since searching a Web site should not be changing anything that the client might care about, and bookmarking or caching the results of a search-engine query is just as useful as bookmarking or caching a static HTML page.
