我们可以在php中创建和使用表单。要获取表单数据,需要使用php超级元组:$_get
和$_post
。
表单请求可以是get
或post
。 要从get
请求中检索数据,需要使用$_get
,而$_post
用于检索post
请求中的数据。
get
请求是表单的默认请求。 通过get
请求传递的数据在url浏览器上是可见的,因此它不太安全。通过 get
请求发送的数据量是有限的,所以发送大量数据不适合使用get请求方法。
下面来看看一个简单的例子,在php中从get请求接收数据。
文件: form1.html
<!doctype html>
<html lang="zh">
<head>
<meta charset="utf-8" />
<title>get表单示例</title>
</head>
<body>
<form action="welcome.php" method="get">
name: <input type="text" name="name"/>
<input type="submit" value="提交"/>
</form>
文件: welcome.php
<?php
$name=$_get["name"];//receiving name field value in $name variable
echo "welcome, $name";
?>
打开浏览器,访问: http://localhost/form1.html , 看到结果如下 -
输入:maxsu
提交后得到以下结果 -
post请求广泛用于提交具有大量数据的表单,例如:文件上传,图像上传,登录表单,注册表单等。
通过post
请求传递的数据在url浏览器上不可见,因此它是安全的。可以通过发送请求发送大量的数据。
下面来看看一个简单的例子,从php中接收来自post
请求的数据。
文件: form1.html
<!doctype html>
<html lang="zh">
<head>
<meta charset="utf-8" />
<title>post表单示例</title>
</head>
<body>
<form action="login.php" method="post">
<table>
<tr><td>name:</td><td> <input type="text" name="name"/></td></tr>
<tr><td>password:</td><td> <input type="password" name="password"/></td></tr>
<tr><td colspan="2"><input type="submit" value="登录"/> </td></tr>
</table>
</form>
文件: login.php
<?php
$name=$_post["name"];//receiving name field value in $name variable
$password=$_post["password"];//receiving password field value in $password variable
echo "welcome: $name, your password is: $password";
?>
打开浏览器,访问: http://localhost/form1.html , 看到结果如下 -
输入:maxsu
和 123456
提交后得到以下结果 -