PHP表單元素介紹之輸入域標記 <input>
輸入域標記 <input>
輸入域標記 <input> 是表單中最常用的標記之一。常用的輸入域標記 <input> 有文本框、按鈕、單選按鈕、複選框等等。
type屬性取值及舉例及說明:
1. text
實例:
<input type="text" name="user" value="user" size="12" maxlength="100">
說明: name 爲文本框的名稱, value 是文本框的默認值,size 指文本框的寬度(以字符爲單位), maxlength 指文本框的最大輸入字符數。
2. password
實例:
pwd:<input type="password" name="pwd" value="123456" size="12" maxlength="16"><br>
密碼域,用戶在該文本框中輸入字符將被替換顯示爲 “ * ” ,以起到保密的作用。
3. file
實例:
file:<input type="file" name="file" enctype="multipart/form-data" size="12" maxlength="100"><br>
說明:文件域,當文件上傳時,可以用來打開一個模式窗口以選擇文件。然後將文件通過表單上傳到服務器,如上傳 word 文件等。必須注意的是,上傳文件時需要指明表單的屬性 enctype="multipart/form-data"纔可以實現上傳功能。
4. radio
實例:
gender:<input type="radio" name="gender" value="0">male <input type="radio" name="gender" value="1">female <input type="radio" name="gender" value="2">unknow
說明:單選按鈕,用於設置一組選擇項,用戶只能選擇一項,checked 屬性用來設置該單選按鈕默認被選中。
5. checkbox
實例:
goods:<input type="checkbox" name="goods" value="0">goods1 <input type="checkbox" name="goods" value="1">goods2 <input type="checkbox" name="goods" value="2">goods3
6. submit
實例:
<input type="submit" name="submit" value="submit">
7. reset
實例:
<input type="reset" name="reset" value="reset">
說明: 清除與重置表單內容,用於清除表單中所有文本框的內容,並使選擇菜單項恢復到初始值。
8. button
實例:
<input type="button" value="i am button">
9.hidden
實例如下所示:
<input type="hidden" name="id">
說明:隱藏域,用於在表單中以隱含方式提交變量值。隱藏域在頁面中對於用戶是不可見的,添加隱藏域的目的在於通過隱藏的方式收集或者發送信息。瀏覽者單擊“發送”按鈕發送表單時,隱藏域的信息也被一起發送到action指定的處理頁。
PHP表單元素介紹之選擇域標記和文字域標記
選擇域標記<select>和<option>
1.列表方式
<select name="lag" id="lag"> <option value="0" selected>php</option> <option value="1">thinkphp</option> <option value="2">laravel</option> </select>
運行結果如下:
2. 菜單方式
<select name="lag" id="lag" multiple> <option value="0" selected>php</option> <option value="1">thinkphp</option> <option value="2">laravel</option> </select>
運行結果如下:
說明:multiple屬性用於菜單列表<select>標記中,指定該選項的用戶可以使用 shift和 ctrl鍵進行多選。
提示: 在上述的說明中只給出了靜態菜單的添加方法,而在web 程序開發過程中,也可以通過循環語句動態的添加菜單項。
文字域標記<textarea>
文字域標記<textarea>用來建立多行的文字域,可以在其中輸入更多的文本。
明: 文本域,也稱多行文本框,用於多行文本的編輯 warp屬性默認爲自動換行方式。
下面通過具體的實例,瞭解 warp屬性 hard 和 soft 換行標記的區別,實例代碼如下:
<html> <form action="" method="post"> <select name="lag" id="lag" multiple> <textarea name="a" cols="20" rows="3" wrap="soft">這是一段文本,用來演示:軟回車</textarea> <textarea name="b" cols="20" rows="3" wrap="hard">這是一段文本,用來演示:硬回車</textarea> <input type="submit" name="submit" value="提交"> </select> </form> </html> <?php error_reporting(0); if($_POST['submit']){ echo nl2br($_POST['a']).'<br>'; echo nl2br($_POST['b']).'<br>'; }
HTML 標記在獲取多行編輯中的字符串時,並不會顯示換行標記。 上面的實例會輸出以下的結果:
PHP表單的創建
創建表單
使用<form>標記,並在其中插入相關的表單信息,即可創建一個表單。表單的結構是如下的形式:
<form name= "form_name" method="method" action="url" enctype="value" target="target_win"> // 插入一些表單元素 <form>
<form>標記屬性如下:
target的屬性值如下所示:
說明:GET()方法是將表單內容附加在URL地址後面發送;POST()方法是將表單中的信息作爲一個數據塊發送到服務器上的處理程序中,在瀏覽器的地址欄不顯示提交的信息。method屬性默認方法爲GET()。