PHP获取表单
· $_GET数组获取GET方式提交的内容
· $_POST数组获取POST方式提交的内容
· $_COOKIE数组获取COOKIE
· $_REQUEST数组获取GET|POST|COOKIE
示例:
1、GET数组获取GET方式提交的内容
HTML 表单:
<!DOCTYPE html>
<html>
<head>
<title>GET 表单示例</title>
</head>
<body>
<h1>用户注册表单</h1>
<form action="get_example.php" method="get">
<label>用户名:</label>
<input type="text" name="username" required><br>
<input type="submit" value="提交">
</form>
</body>
</html>
get_example.php:
<?php // 获取用户名 $username = $_GET['username']; // 输出用户名 echo "用户名:". $username; ?>
2、使用 $_POST 获取 POST 方式提交的内容:
HTML 表单:
<!DOCTYPE html>
<html>
<head>
<title>POST 表单示例</title>
</head>
<body>
<h1>用户注册表单</h1>
<form action="post_example.php" method="post">
<label>用户名:</label>
<input type="text" name="username" required><br>
<input type="submit" value="提交">
</form>
</body>
</html>
post_example.php:
<?php // 获取用户名 $username = $_POST['username']; // 输出用户名 echo "用户名:". $username; ?>
3、使用 $_COOKIE 获取 COOKIE:
set_cookie.php:
<?php
// 设置一个名为 "username" 的 COOKIE
setcookie("username", "John Doe", time() + 3600);
?>
get_cookie.php:
<?php // 获取名为 "username" 的 COOKIE $username = $_COOKIE["username"]; // 输出用户名 echo "用户名:". $username; ?>
4、使用 $_REQUEST 获取 GET|POST|COOKIE:
HTML 表单:
<!DOCTYPE html>
<html>
<head>
<title>请求示例</title>
</head>
<body>
<h1>用户注册表单</h1>
<form action="request_example.php" method="post">
<label>用户名:</label>
<input type="text" name="username" required><br>
<input type="submit" value="提交">
</form>
<a href="request_example.php?username=John+Doe">直接访问</a>
</body>
</html>
request_example.php:
<?php
// 获取 GET 请求的用户名
if (isset($_GET['username'])) {
$username = $_GET['username'];
}
// 获取 POST 请求的用户名
if (isset($_POST['username'])) {
$username = $_POST['username'];
}
// 获取 COOKIE 中的用户名
if (isset($_COOKIE['username'])) {
$username = $_COOKIE['username'];
}
// 输出用户名
echo "用户名:". $username;
?>