Blogger Information
Blog 22
fans 0
comment 0
visits 11772
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
2020.1.8PHP表单验证与流程
Original
532 people have browsed it
2020-01-081. 流程控制之循环 for()while()foreach() 2. PHP表单处理 请求类型请求变量的验证// 几个实用函数// count(): 计算数组元素的数量// echo '数组元素的数量是: ' . count($arr1) . ' 个元素<br>';// strlen(): 计算字符串长度// echo '字符串长度: ' . strlen('php.cn') . ' 个字符<br>';// trim(), rtrim(), ltrim(): 是从字符串二边,右边, 左边删除指定的字符,默认删除的是空格$str = ' php.cn ';// echo '字符串原始长度: ' . strlen($str) . ' 个字符<br>';$str = trim($str);// echo '字符串当前长度: ' . strlen($str) . ' 个字符<br>';$str = ltrim($str, 'p');// echo '字符串当前长度: ' . strlen($str) . ' 个字符<br>';// echo $str . '<br>';$str = rtrim($str, '.cn');// echo '字符串当前长度: ' . strlen($str) . ' 个字符<br>';// echo $str . '<br>';// mt_rand(min, max): 产生指定范围的随机数 <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>用户注册</title> <style> .register { width: 300px; display: flex; flex-direction: column; color: #555; font-size: 14px; margin: auto; } .register>h3 { align-self: center; border-bottom: 3px solid lightcoral; font-weight: normal; font-size: 20px; } /* 表单样式 */ .register>form { display: flex; flex-direction: column; padding: 20px; background-color: lightblue; border-radius: 20px; } .register>form:hover { box-shadow: 0 0 8px #888; } /* 表单中所有inpu控件样式 */ .register>form input { border: none; outline: none; border-radius: 5px; padding-left: 5px; } .register>form input:hover { background-color: lightgoldenrodyellow; } /* 表单中所有子元素转flex,便于设置对齐 */ .register>form>span { margin: 5px 0; display: flex; justify-content: space-between; } /* 按钮按钮所在元素转flex,便于设置 */ .register>form>span:last-of-type { display: flex; justify-content: center; } /* 提交按钮样式 */ .register>form>span:last-of-type > button { margin-top: 5px; background-color: #888; border: none; background-color: lightcoral; color: white; width: 100px; height: 30px; font-size: 16px; } /* 提交按钮的悬停样式 */ .register>form>span:last-of-type > button:hover { background-color: seagreen; cursor: pointer; } </style></head><body> <div class="register"> <h3>用户注册</h3> <!-- method: 默认就是get请求 --> <form action="demo3.php" method="post"> <span> <label for="username">用户名:</label> <input type="text" name="username" id="username" placeholder="不超过20个字符" required autofocus> </span> <span> <label for="password1">密码:</label> <input type="password" name="password1" id="password1" placeholder="不能为空" required> </span> <span> <label for="password2">重复密码:</label> <input type="password" name="password2" id="password2" placeholder="必须与上面一致" required> </span> <span> <label for="email">邮箱:</label> <input type="email" name="email" id="email" placeholder="不能为空" required> </span> <span> <label for="male">性别:</label> <span> <input type="radio" name="gender" id="male" value="male" required><label for="male">男</label> <input type="radio" name="gender" id="female" value="female" required><label for="female">女</label> <input type="radio" name="gender" id="secret" value="secret" required checked><label for="secret">保密</label> </span> </span> <span> <label for="js">关心:</label> <span> <input type="checkbox" name="likes[]" id="html" value="1"><label for="html">HTML</label> <input type="checkbox" name="likes[]" id="css" value="2" checked><label for="css">CSS</label> <input type="checkbox" name="likes[]" id="js" value="3"><label for="js">JS</label> <input type="checkbox" name="likes[]" id="php" value="4" checked><label for="php">PHP</label> </span> </span> <span> <button>提交</button> </span> </form> </div></body></html> <?php// 超全局变量: // 1. 是指每一个php程序(就是一个动态的php页面)都已经存在的变量, 不需要用户去主动创建// 2. 它的值, 是由系统根据每个php程序自动设置初始值, 大部分是反映程序状态// 3. 它的值, 大多是允许用户更新, 但不能删除它, 否则会引起致使错误 // 4. 它没有作用域限制, 无论是全局, 还是函数中, 都直接用, 不需要声明// 5. 因为每一个页拿来就用, 很容易让初学者产生错觉: 它是跨页面, 不行, 只是每个页都已存在// $_REQUEST: 请求数据的超全局变量,里面保存的是用户所有的请求数据// 包括get , post , cookie// echo '<pre>' . print_r($_REQUEST, true) . '';// echo '<pre>' . print_r($_POST, true) . '';// echo '用户名: ' . $_POST['username'] . '<br>';// echo '邮箱: ' . $_POST['email'] . '<br>';// 判断用户的请求类型是否合法// echo '请求类型: ' . $_SERVER['REQUEST_METHOD'] . '<br>';if ($_SERVER['REQUEST_METHOD'] === 'POST') { // 检查请求变量是否设置, 并且值不能为NULL, isset() // if (isset($_POST['username'])) { // echo '有'; // } else { // echo '无'; // } // empty(): 空字符串, 0, null, false // if (!empty($_POST['username'])) { // echo '有'; // } else { // echo '无'; // } // isset(): 用在设置请求变量默认值 // echo $_POST['grade']; // if (!isset($_POST['grade'])) { // $_POST['grade'] = 60; // } // echo $_POST['grade']; // 三元运算符, 将双分支进行简化 // 条件 ? true : false // $_POST['grade'] = isset($_POST['grade']) ? $_POST['grade'] : 70; // echo $_POST['grade']; // $grade = isset($_POST['grade']) ? $_POST['grade'] : 80; // // php7+ // $grade = $_POST['grade'] ?? 90; // echo $grade; // if (!empty($_POST['username'])) { // $username = $_POST['username']; // } if (!empty($_POST['username'])) $username = $_POST['username']; if (!empty($_POST['password1'])) $password1 = $_POST['password1']; if (!empty($_POST['password2'])) $password2 = $_POST['password2']; if (!empty($_POST['email'])) $email = $_POST['email']; if (!empty($_POST['gender'])) $gender = $_POST['gender']; if (!empty($_POST['likes'])) $likes = $_POST['likes']; // 二次密码必须一致 if ($password1 === $password2) { // md5():32位随机字符串, sha1():40位随机字符串 $password = md5(sha1($password1)); } else { exit('<script>alert("二次密码不一致");history.back();</script>'); } $data = compact('username', 'password', 'email', 'gender', 'likes'); echo '<pre>' . print_r($data, true) . '';} else { exit('<h3 style="color:red">请求类型错误!</h3>');}
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post