PHP中用post方法获取HTML表单数据

类别:PHP      发布日期:2010-04-03

表单是我们在开发过程中经常使用到的。如何获取表单中的数据?请看本实例。
获取文本框、单选按钮、下拉列表的值,我们可以用$_POST['控件的name属性']来得到。
复选框checkbox,也是利用name属性,不过我们要将name写成数组的形式。
运行下面两端代码的时候,首先将其放到同一个abc.php文件中。
复制内容到剪贴板折叠XML/HTML 代码
  1. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  2. <form action="abc.php" method="post">  
  3.     <table>  
  4.         <tr>  
  5.             <td>  
  6.                 用户名:<input type="text" name="wenbenkuang" />  
  7.             </td>  
  8.         </tr>  
  9.         <tr>  
  10.             <td>  
  11.                 性别:  
  12.                 <input type="radio" name="danxuan" id="man" value="男" checked="checked"/><label for="man"></label>  
  13.                 <input type="radio" name="danxuan" id="woman" value="女"/><label for="woman"></label>  
  14.             </td>  
  15.         </tr>  
  16.         <tr>  
  17.             <td>  
  18.                 爱好:  
  19.                 <input type="checkbox" name="chk[]" value="读书" id="reading"/><label for="reading">读书</label>  
  20.                 <input type="checkbox" name="chk[]" value="音乐" id="music"/><label for="music">音乐</label>  
  21.             </td>  
  22.         </tr>  
  23.         <tr>  
  24.             <td>  
  25.                 所在地:  
  26.                 <select name="address">  
  27.                     <option value="烟台">烟台</option>  
  28.                     <option value="济南">济南</option>  
  29.                     <option value="青岛">青岛</option>  
  30.                 </select>  
  31.             </td>  
  32.         </tr>  
  33.         <tr>  
  34.             <td colspan="2" align="center">  
  35.                 <input type="hidden" name="hid" value="hid" />  
  36.                 <input type="submit" name="sub" value="提交" />  
  37.             </td>  
  38.         </tr>  
  39.     </table>  
  40. </form>  
复制内容到剪贴板折叠PHP 代码
  1. <?php  
  2.     if($_POST['hid']=='hid')  
  3.     {  
  4.         echo "用户名是:".$_POST['wenbenkuang']."<br/>";  
  5.         echo "性别是:".$_POST['danxuan']."<br/>";  
  6.         $chk=$_POST['chk'];  
  7.         $cnt=count($_POST['chk']);  
  8.         echo "爱好是:";  
  9.         if($cnt)  
  10.         {  
  11.             foreach($chk as $value)  
  12.             {  
  13.                 echo $value." ";  
  14.             }  
  15.         }else  
  16.         {  
  17.             echo "没有选择";  
  18.         }  
  19.         echo "<br/>所在地:".$_POST['address'];  
  20.         //exit();  
  21.     }  
  22. ?>  

/* */
暂无留言
留 言 板
您的鼎鼎大名:
您的金玉良言:
验证码: 验证码