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