PHP HTML表单

2018-02-22 16:40 更新

PHP教程 - PHP HTML表单

HTML表单是嵌入在页面中的HTML元素的集合。通过添加不同类型的元素,您可以创建不同的表单字段,例如文本字段,下拉菜单,复选框等。

所有Web表单都以开头< form> 标签,并以结束< / form> 标签:

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

表单属性

在开头< form>中有两个属性。 标签:

  • 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

要从表单中读取数据,我们可以使用以下内容三个超全局变量。

超全局数组 描述
$_GET 包含表单使用get方法发送的所有字段名称和值
$_POST 包含表单使用post方法发送的所有字段名称和值
$_REQUEST 包含组合的$ _GET和$ _POST数组的值,以及$ _COOKIE超全局数组的值

这三个超全局数组中的每一个包含从发送形式作为数组键的字段名称该字段将其自身作为数组值。

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

然后,您可以使用或访问用户在表单字段中输入的值$ _GET或$ _REQUEST超全局:

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


GET和POST不同

GET 在URL中发送其变量。很容易看到 GET 发送的内容。用户可以更改发送的内容对于可以在网址中发送的字符数,通常有一个下限。如果我们尝试使用GET发送长变量,我们可能会丢失一些。

POST 在幕后发送它的变量,并有一个更高的限制。它的限制通常是几兆字节。

浏览器不会自动重新发送发布数据,如果您的用户单击后退按钮,您可能会收到一条消息“此页上的数据需要重新发送"。这不会发生在 GET ,浏览器将只通过URL重新发送数据。

PHP在php.ini中有 post_max_size 条目。它通常设置为8M默认情况下,这是8兆字节。

实施例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> 
以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号