Blogger Information
Blog 40
fans 0
comment 0
visits 16110
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
实现用户注销, 封装表单字段的原生验证方法 , 定界符heredoc, nowdoc的用法与使用
飞天001
Original
335 people have browsed it

实现用户注销, 封装表单字段的原生验证方法 , 定界符heredoc, nowdoc的用法与使用

1.用户注销功能

  1. //创建点击事件
  2. <a href='' onclick="logout(event)">[注销]</a>
  1. //方法实现
  2. async function logout(ev){
  3. ev.preventDefault();
  4. // ev.preventDefault();
  5. if(confirm('是否退出')){
  6. const url = './lib/userHandle.php?action=logout';
  7. const res = await fetch(url);
  8. const result = await res.json();
  9. if(result){
  10. alert('退出成功');
  11. location.href='index.php';
  12. }else{
  13. alert('系统错误,请重试');
  14. location.href='login.php';
  15. }
  16. }
  17. }
  1. session_start();
  2. $flag = false;
  3. if(session_destroy()){
  4. $flag = true;
  5. }
  6. echo json_encode($flag);

2. 封装表单字段的原生验证方法

  1. const getInput = (form)=>{
  2. return {
  3. nackname:{
  4. ele:form.nickname,
  5. value:form.nickname.value.trim()
  6. },email:{
  7. ele:form.email,
  8. value:form.email.value.trim()
  9. },password:{
  10. ele:form.password,
  11. value:form.password.value.trim()
  12. },rePassword:{
  13. ele:form.rePassword,
  14. value:form.rePassword.value.trim()
  15. }
  16. }
  17. }
  1. const isEmpty = (user)=>{
  2. switch(true){
  3. case user.nickname.value.length ===0:
  4. alert('昵称不能为空');
  5. user.nickname.ele.focus();
  6. return false; //代码结束,不再继续执行
  7. case user.email.value.length ===0:
  8. alert('邮箱不能为空');
  9. user.email.ele.focus();
  10. return false;
  11. case user.password.value.length===0:
  12. alert('密码不能为空');
  13. user.password.ele.focus();
  14. return false;
  15. case user.rePassword.value.length ===0:
  16. alert('确认密码不能为空');
  17. user.rePassword.ele.focus();
  18. return false;
  19. default:
  20. return true; //拦截结束,返回true
  21. }
  22. }
  1. const isPwdEqeu = (user)=>{
  2. if(user.password.value!==user.rePassword.value){
  3. alert('两次密码输入不一致');
  4. user.password.focus(); //输入框
  5. return false;
  6. } else{
  7. return true;
  8. }
  9. }
  1. const createData = (user)=>{
  2. return{
  3. nickname:user.nickname.value,
  4. email:user.email.value,
  5. password:user.password.value
  6. }
  7. }
  1. async function insertData(data){
  2. url = './lib/userHandle.php?action=register';
  3. const res = await fetch(url,{
  4. method:'POST',
  5. headers:{
  6. 'content-Type':'application/json;charset=utf-8',
  7. },
  8. body:JSON.stringify(data)
  9. });
  10. //返回数据
  11. const result = res.json();
  12. if(result){
  13. aletrt('注册成功');
  14. location.href='index.php';
  15. }else{
  16. alert('注册失败,请重试');
  17. location.href='register.php';
  18. btn.form.nickname.focus();
  19. }
  20. }

3. 定界符heredoc, nowdoc的用法与使用

  1. echo <<<TIPS
  2. <script>
  3. alert('非法请求!')
  4. location.href='../login.php'
  5. <script>
  6. TIPS;
  1. // 定界符 heredoc 解析变量和特殊字符
  2. $str = <<< POEM
  3. 窗前明月光, \n疑是地上霜。\n
  4. POEM;
  5. // 定界符 nowdoc //不会解析变量和特殊字符
  6. $str = <<< 'POEM'
  7. 窗前明月光, \n疑是地上霜。\n
  8. POEM;
Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
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