-
from表单第二部分
1、提交地址
action提交地址是指这个表单填写完后提交,发送的请求地址是什么
从便于维护的角度考虑,一般我们都是提交给当前文件,然后在文件中判断是否是表单请求
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
//表单提交请求
}
另外,建议使用下面的方式动态获取当前页面的访问路径,这样能避免因为文件重命名或网站目录结构的调整而修改代码了:
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method = 'post'>
2、提交方式
method可以用于设置表单的提交方式,目前我们接触到的两种最为常见的是GET和POST
从效果来看,两者都可以将数据提交到服务端,但是从实现提交的原理来说两者有很大不同:
GET:
表单数据通过URL中的?参数传递到服务端的
POST
表单数据通过请求体传递到服务端的
两者的使用原则:
应该想清楚这次请求主要是去拿东西,还是去送东西
3、常见表单元素处理
表单元素中的文本框文本域一类的元素,都是直接将元素的name属性值作为键,用户填写的信息作为值,发送到服务端。
但是表单元素中还有一些比较特殊的表单元素需要单独考虑
3.1 单选按钮
<!-- 最终只会提交选中的value -->
<input type="radio" name='gender' value="male">男
<input type="radio" name = 'gender' value="famale">女
<button>提交</button>
3.2 复选按钮
<!-- 没有设置value的时候提交on,设置了value的时候提交value的值 -->
<input type="checkbox" name="agree">
<input type="checkbox" name="agree" value="agree">
<!-- 如果需要提交多个选中项,则可以在name后面加上[],最终提交到服务端,接收到的是一个索引数组 -->
<input type="checkbox" name="fun[]" value="football">
<input type="checkbox" name="fun[]" value="baseketball">
<input type="checkbox" name="fun[]"value ='earth'>
3.3 选择框
<select name="select" >
<!-- 设置了value的提交value -->
<option value="1"></option>
<!-- 没有设置value的提交innnerText -->
<option >数学</option>
</select>
3 案例
- 基于文件的注册和登录
1、注册
1、用户请求一个注册页面
2、服务端返回一个注册表单
3、用户填写表单并提交
4、服务端接收用户提交的信息
5、判断是否正确填写内容及是否勾选同意
6、如果出现异常界面给出提示,并返回表单
7、如果都正常,则保存到文件中(每个用户一行)
<?php
//接受用户提交的数据保存到文件中
//表单处理三部曲
//1.接受并校验
//2.持久化(把数据持久保存到磁盘)
//3.响应(服务端的反馈)
//判断用户是否提交了表单请求
//
function postback(){
global $message;
if (empty($_POST['username'])) {
$message = '会不会玩';
return;
}
if (empty($_POST['confirm'])) {
$message = '请确认密码';
return;
}
if (empty($_POST['password'])) {
$message = '请输入密码';
return;
}
if ($_POST['password'] !== $_POST['confirm']) {
$message = '两次密码输入不一致';
return;
}
if (!(isset($_POST['agree']) && $_POST['agree'] == 'on')) {
$message = '必须同意注册协议';
return;
}
//所有校验都OK
$username = $_POST['username'];
$password = $_POST['password'];
$message = "注册成功";
//将文件保存到指定的文件中
file_put_contents('users.txt', $username . '|' . $password . "\n" FILE_APPEND);
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
postback();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method= 'post'>
<table border="1">
<tr>
<td><label for="username">用户名</label></td>
<td><input type="text" id="username" name="username" value="<?php echo isset($_POST['username']) ? $_POST['username'] : ''; ?>"></td>
</tr>
<tr>
<td><label for="password">密码</label></td>
<td><input type="password" id="password" name="password"></td>
</tr>
<tr>
<td><label for="confirm">确认密码</label></td>
<td><input type="confirm" id="confirm" name="confirm"></td>
</tr>
<tr>
<td></td>
<td><input type="checkbox" name="agree">同意注册协议</td>
</tr>
<?php if (isset($message)): ?>
<tr>
<td></td>
<td><?php echo $message; ?></td>
</tr>
<?php endif ?>
<tr>
<td></td>
<td><button>注册</button></td>
</tr>
</table>
</form>
</body>
</html>
4 文件上传
<?php
function postback(){
//var_dump($_FILES['avatar']);
if (!isset($_FILES['avatar'])) {
$GLOBALS['message'] = '别玩我了';
return;
}
$avatar = $_FILES['avatar'];
//var_dump($avatar);
//array(5) {
//["name"]=>
// string(16) "表单过程.jpg"
// ["type"]=>
// string(10) "image/jpeg"
// ["tmp_name"]=>
// string(27) "C:\Windows\Temp\php97D4.tmp"
// ["error"]=>
// int(0)
// ["size"]=>
// int(61480)
//}
if ($avatar['error'] !== UPLOAD_ERR_OK) {
$GLOBALS['message'] = '上传失败';
return;
}
//接收到了文件,将文件从临时目录移动到网站根目录中去
$source = $avatar['tmp_name']; //源文件在哪
$target = 'load/' . $avatar['name']; //目标放在哪
$move = move_uploaded_file($source, $target);
if (!$move) {
$GLOBALS['message'] = '上传失败';
return;
}
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
postback();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method = "post" enctype="multipart/form-data">
<input type="file" name="avatar">
<button>提交</button>
<?php if (isset($message)): ?>
<p style="color: red"><?php echo $message ?></p>
<?php endif ?>
</form>
</body>
</html>