PHP $_REQUEST
2020-04-23 本文已影响0人
887d1fc86fe6
- $_REQUEST
代表浏览器通过 "get" 方式或 "post" 方式提交的数据合集。
即:它既能接收到 get 过来的数据,也能接收到 post 过来的数据!
通常,一个表单,只提交一种形式的数据,要么get数据,要么post数据!
也就是在接受数据的时候我们不需要管它 get 或 post 过来的数据:
$_GET['num1'] // 取出 get 过来的数据
$_POST['num2'] // 取出 post 过来的数据
$_REQUEST['num1'] // 取出 get 过来的数据
$_REQUEST['num2'] // 取出 post 过来的数据
- demo 使用:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<?php
// 以防首次进来在 html 中使用对象报错未定义
$num2 = '';
$num2 = '';
$result = '';
// 程序默认是从上到下,首次进来会直接执行这段代码,所以需要检查是否有值
if (isset($_REQUEST['num1'])) {
$num1 = $_REQUEST['num1'];
$num2 = $_REQUEST['num2'];
$result = $num1 + $num2;
$id = $_REQUEST['id'];
$userName = $_REQUEST['userName'];
echo '用户名', $userName;
}
?>
<body>
<!-- action 留空就标识提交给自己(本页) -->
<!-- <form action="" method="post"> -->
<form action="" method="get">
<input type="text" name="num1" value="<?php echo $num1 ?>">
+
<input type="text" name="num2" value="<?php echo $num2 ?>">
<input type="submit" value="=">
<input type="text" name="result" value="<?php echo $result ?>">
</form>
<!-- 这种表单形式才可以同时具备提交get跟post数据, action地址栏?后面为 get 数据,form 表单提交的为 post 数据 -->
<form action="06-from_request.php?id=10&userName=dzm" method="post">
<input type="text" name="num1" value="<?php echo $num1 ?>">
+
<input type="text" name="num2" value="<?php echo $num2 ?>">
<input type="submit" value="=">
<input type="text" name="result" value="<?php echo $result ?>">
</form>
</body>
</html>
- demo 效果: