PHP HTML表單

2018-02-22 16:40 更新

PHP教程 - PHP HTML表單

HTML表單是嵌入在頁面中的HTML元素的集合。通過添加不同類型的元素,您可以創(chuàng)建不同的表單字段,例如文本字段,下拉菜單,復(fù)選框等。

所有Web表單都以開頭< form> 標(biāo)簽,并以結(jié)束< / form> 標(biāo)簽:

<form action="myscript.php" method="POST">  
     <!-- Contents of the form go here -->  
</form>  

表單屬性

在開頭< form>中有兩個屬性。 標(biāo)簽:

  • action - tells the browser where to send the form data. This should either be an absolute URL or a relative URL.
  • method - tells the browser how to send the form data. You can use two methods: GET is for sending small amounts of data and makes it easy for the user to resubmit the form, and POST can send much larger amounts of form data.
<form action="someform.php" method="post">
  Name: <input type="text" name="Name" value="Jim" /><br />
  Password: <input type="password" name="Password" /><br />
  Age: <input type="text" name="Age" /><br />
  <input type="submit" />
</form>


PHP GET和POST

要從表單中讀取數(shù)據(jù),我們可以使用以下內(nèi)容三個超全局變量。

超全局?jǐn)?shù)組 描述
$_GET 包含表單使用get方法發(fā)送的所有字段名稱和值
$_POST 包含表單使用post方法發(fā)送的所有字段名稱和值
$_REQUEST 包含組合的$ _GET和$ _POST數(shù)組的值,以及$ _COOKIE超全局?jǐn)?shù)組的值

這三個超全局?jǐn)?shù)組中的每一個包含從發(fā)送形式作為數(shù)組鍵的字段名稱該字段將其自身作為數(shù)組值。

<input type="text "  name="emailAddress" value="" />  

然后,您可以使用或訪問用戶在表單字段中輸入的值$ _GET或$ _REQUEST超全局:

$email = $_GET["emailAddress"]; 
$email = $_REQUEST["emailAddress"];     


GET和POST不同

GET 在URL中發(fā)送其變量。很容易看到 GET 發(fā)送的內(nèi)容。用戶可以更改發(fā)送的內(nèi)容對于可以在網(wǎng)址中發(fā)送的字符數(shù),通常有一個下限。如果我們嘗試使用GET發(fā)送長變量,我們可能會丟失一些。

POST 在幕后發(fā)送它的變量,并有一個更高的限制。它的限制通常是幾兆字節(jié)。

瀏覽器不會自動重新發(fā)送發(fā)布數(shù)據(jù),如果您的用戶單擊后退按鈕,您可能會收到一條消息“此頁上的數(shù)據(jù)需要重新發(fā)送"。這不會發(fā)生在 GET ,瀏覽器將只通過URL重新發(fā)送數(shù)據(jù)。

PHP在php.ini中有 post_max_size 條目。它通常設(shè)置為8M默認(rèn)情況下,這是8兆字節(jié)。

實施例2

將以下文件命名為index.htm文件。

       <!DOCTYPE html5>
       <html> 
         <body> 
           <form action="index.php" method="post"> 
               <label for="firstName">First name</label> 
               <input type="text" name="firstName" id="firstName" value="" /> 

               <label for="lastName">Last name</label> 
               <input type="text" name="lastName" id="lastName" value="" /> 

               <label for="password1">Choose a password</label> 
               <input type="password" name="password1" id="password1" value="" /> 
               <label for="password2">Retype password</label> 
               <input type="password" name="password2" id="password2" value="" /> 

               <label for="genderMale">Are you male...</label> 
               <input type="radio" name="gender" id="genderMale" value="M" /> 
               <label for="genderFemale">...or female?</label> 
               <input type="radio" name="gender" id="genderFemale" value="F" /> 

               <label for="favoriteWidget">What"s your favorite widget?</label> 
               <select name="favoriteWidget" id="favoriteWidget" size="1"> 
                 <option value="superWidget">The SuperWidget</option> 
                 <option value="megaWidget">The MegaWidget</option> 
                 <option value="wonderWidget">The WonderWidget</option> 
               </select> 

               <label for="newsletter">Do you want to receive our newsletter?</label> 
               <input type="checkbox" name="newsletter" id="newsletter" value="yes" /> 

               <label for="comments">Any comments?</label> 
               <textarea name="comments" id="comments" rows="4" cols="50"> </textarea> 
               <div style="clear: both;"> 
                 <input type="submit" name="submitButton" id="submitButton"  value="Send Details" /> 
                 <input type="reset" name="resetButton" id="resetButton" value="Reset Form" style="margin-right: 20px;" /> 
               </div> 
           </form> 
         </body> 
       </html> 

接下來,將以下腳本作為index.php保存在與index.html相同的文件夾中。

<!DOCTYPE html5> 
<html> 
  <body> 
    <p>Thank you for registering. Here is the information you submitted:</p> 
    <dl> 
      <dt>First name</dt><dd><?php echo $_POST["firstName"]?></dd> 
      <dt>Last name</dt><dd><?php echo $_POST["lastName"]?></dd> 
      <dt>Password</dt><dd><?php echo $_POST["password1"]?></dd> 
      <dt>Retyped password</dt><dd><?php echo $_POST["password2"]?></dd> 
      <dt>Gender</dt><dd><?php echo $_POST["gender"]?></dd> 
      <dt>Favorite widget</dt><dd><?php echo $_POST["favoriteWidget"]?></dd> 
      <dt>Do you want to receive our newsletter?</dt><dd><?php echo $_POST["newsletter"]?></dd> 
      <dt>Comments</dt><dd><?php echo $_POST["comments"]?></dd> 
    </dl> 
  </body> 
</html> 
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號