一、检索表单信息
  PHP 中的 $_GET 和 $_POST 变量用于检索表单中的信息,比如用户输入
  提交的表单:
    <form action="welcome.php" method="post">
      名字: <input type="text" name="fname">
      年龄: <input type="text" name="age">
      <input type="submit" value="提交">
    </form>

  检索表单信息:
    欢迎<?php echo $_POST["fname"]; ?>!<br>
    你的年龄是 <?php echo $_POST["age"]; ?>

二、下拉菜单(单选)
    <form action="" method="get">
      <select name="q">
        <option value="">选择一个站点:</option>
        <option value="RUNOOB">Runoob</option>
        <option value="GOOGLE">Google</option>
        <option value="TAOBAO">Taobao</option>
      </select>
      <input type="submit" value="提交">
    </form>
三、检索下拉菜单的选值(单选)
    
<?php
      $q = isset($_GET['q'])? htmlspecialchars($_GET['q']) : '';
      if($q) {
        if($q =='RUNOOB') {
          echo '菜鸟教程<br>http://www.runoob.com';
        } else if($q =='GOOGLE') {
          echo 'Google 搜索<br>http://www.google.com';
        } else if($q =='TAOBAO') {
          echo '淘宝<br>http://www.taobao.com';
        }
      } else {
    ?>
      <form action="" method="get">
        <select name="q">
          <option value="">选择一个站点:</option>
          <option value="RUNOOB">Runoob</option>
          <option value="GOOGLE">Google</option>
          <option value="TAOBAO">Taobao</option>
        </select>
        <input type="submit" value="提交">
       </form>
    <?php
      }
    ?>
    注意:表单和php脚本在if...else语句中的书写位置

四、下拉菜单
(多选)
    
    <form action="" method="post">
      <select multiple="multiple" name="q[]">
        <option value="">选择一个站点:</option>
        <option value="RUNOOB">Runoob</option>
        <option value="GOOGLE">Google</option>
        <option value="TAOBAO">Taobao</option>
      </select>
      <input type="submit" value="提交">
    </form>

五、检索下拉菜单的选值(多选)

    <?php
      $q = isset($_POST['q'])? $_POST['q'] : '';
      if(is_array($q)) {
        $sites = array(
          'RUNOOB' => '菜鸟教程: http://www.runoob.com',
          'GOOGLE' => 'Google 搜索: http://www.google.com',
          'TAOBAO' => '淘宝: http://www.taobao.com',
        );
        foreach($q as $val) {
          // PHP_EOL 为常量,用于换行
          echo $sites[$val] . PHP_EOL;
        }

      } else {
    ?>
        <form action="" method="post">
          <select multiple="multiple" name="q[]">
            <option value="">选择一个站点:</option>
            <option value="RUNOOB">Runoob</option>
            <option value="GOOGLE">Google</option>
            <option value="TAOBAO">Taobao</option>
          </select>
          <input type="submit" value="提交">
        </form>
    <?php
      }
    ?>

 六、单选按钮表单    

    <?php
      $q = isset($_GET['q'])? htmlspecialchars($_GET['q']) : '';
      if($q) {
        if($q =='RUNOOB') {
          echo '菜鸟教程<br>http://www.runoob.com';
        } else if($q =='GOOGLE') {
          echo 'Google 搜索<br>http://www.google.com';
        } else if($q =='TAOBAO') {
          echo '淘宝<br>http://www.taobao.com';
        }
      } else {
    ?>    

      <form action="" method="get">

        <input type="radio" name="q" value="RUNOOB" />Runoob

        <input type="radio" name="q" value="GOOGLE" />Google

        <input type="radio" name="q" value="TAOBAO" />Taobao
        <input type="submit" value="提交">
      </form>
    <?php
      }
    ?>

 七、复选框   

    <?php
      $q = isset($_POST['q'])? $_POST['q'] : '';
      if(is_array($q)) {
        $sites = array(
          'RUNOOB' => '菜鸟教程: http://www.runoob.com',
          'GOOGLE' => 'Google 搜索: http://www.google.com',
          'TAOBAO' => '淘宝: http://www.taobao.com',
        );
        foreach($q as $val) {
          // PHP_EOL 为常量,用于换行
          echo $sites[$val] . PHP_EOL;
        }

      } else {
     ?>

        <form action="" method="post">
          <input type="checkbox" name="q[]" value="RUNOOB"> Runoob<br>
          <input type="checkbox" name="q[]" value="GOOGLE"> Google<br>
          <input type="checkbox" name="q[]" value="TAOBAO"> Taobao<br>
          <input type="submit" value="提交">
        </form>
     <?php
      }
     ?>

 八、表单验证   

    <?php
      // 定义变量并默认设置为空值
      $nameErr = $emailErr = $genderErr = $websiteErr = "";
      $name = $email = $gender = $comment = $website = "";

      if ($_SERVER["REQUEST_METHOD"] == "POST")
      {
        if (empty($_POST["name"]))
        {
          $nameErr = "名字是必需的";
        }
        else
        {
          $name = test_input($_POST["name"]);
          // 检测名字是否只包含字母跟空格
          if (!preg_match("/^[a-zA-Z ]*$/",$name))
          {
            $nameErr = "只允许字母和空格";
          }
        }

        if (empty($_POST["email"]))
        {
          $emailErr = "邮箱是必需的";
        }
        else
        {
          $email = test_input($_POST["email"]);
          // 检测邮箱是否合法
          if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
          {
            $emailErr = "非法邮箱格式";
          }
        }

        if (empty($_POST["website"]))
        {
          $website = "";
        }
        else
        {
          $website = test_input($_POST["website"]);
          // 检测 URL 地址是否合法
          if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website))
          {
            $websiteErr = "非法的 URL 的地址";
          }
        }

        if (empty($_POST["comment"]))
        {
          $comment = "";
        }
        else
        {
          $comment = test_input($_POST["comment"]);
        }

        if (empty($_POST["gender"]))
        {
          $genderErr = "性别是必需的";
        }
        else
        {
          $gender = test_input($_POST["gender"]);
        }
      }

      function test_input($data)
      {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
      }
    ?>