表单操作

$_GET

所有表单输入的数据被加载到请求的URL地址后面;
如:test.php?username=free&password=123&content=dfdsfsfd;
GET方式提交数据只能传递文本,能够提交的数据量大小有限,安全性差;

$_POST

POST提交数据的方式把表单的数据打包放入http请求中;
POST能够提交更多的数据;

接收数据

表单提交的数据会自动封装为数组;     
用$_GET, $_POST, 或$_REQUEST获得表单提交的数据;

处理多值表单控件

多值表单控件(如复选框和多选框),大大提高了基于web的数据收集能力;
因为这些组件是多值的,所以表单处理函数必须能够识别一个表单变量中可能有
多个值;为了让php识别一个表单变量的多个值(即考虑为数组),需要对表单名
(元素的name属性值)增加一对中括号,如:

<input type="checkbox" name="love[]" />

$_REQUEST

$_REQUEST 支持两种方式发送过来的请求,即 post 和 get 它都可以接受,显示不显示要看传递方法。
get 会显示在 url 中(有字符数限制),post 不会在 url 中显示,可以传递任意多的数据(只要服务器支持)

代码示例

HTML代码
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <form action="register.php" method="post">
        <table>
            <tr>
                <td>用户名:</td>
                <td><input type="text" name="username" placeholder="请输入用户名" required></td>
            </tr>
            <tr>
                <td>密码:</td>
                <td><input type="password" name="pwd" placeholder="请输入密码" required></td>
            </tr>
            <tr>
                <td>爱好</td>
                <td>
                    <input type="checkbox" name="love[]" value="1">看电影
                    <input type="checkbox" name="love[]" value="2">玩游戏
                    <input type="checkbox" name="love[]" value="3">写代码
                </td>
            </tr>
            <tr>
                <td></td>
                <td><input type="submit" value="登陆"></td>
            </tr>
        </table>
    </form>
</body>

</html>
php代码
<?php
    // 接收POST提交过来的数据
    print_r($_POST);
?>
powered by GitbookEdit Time: 2023-04-08 10:28:32