Blogger Information
Blog 59
fans 6
comment 0
visits 52205
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
使用Ajax-Json-POST请求,做一个数据表进行用户信息验证-js-38课8.12
希望
Original
663 people have browsed it

1.创建简单的表单

2.获取表单和按钮

3.给按钮绑定事件

  • 判断邮箱或密码是否正确,给出不同的提示

4.创建demo7.html

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>ajax-json-formdata用户信息验证</title>
  7. </head>
  8. <body>
  9. <p>请登录:</p>
  10. <form
  11. action=""
  12. method="POST"
  13. style="display: grid; gap: 15px;"
  14. onsubmit="return false"
  15. >
  16. <input
  17. type="email"
  18. name="email"
  19. placeholder="exam@email.com"
  20. required
  21. autofocus
  22. />
  23. <input type="password" name="password" placeholder="******" required />
  24. <button>提交</button>
  25. </form>
  26. <script>
  27. // 1. 获取表单和按钮
  28. var form = document.querySelector("form");
  29. var btn = document.querySelector("form button");
  30. // 2. 给按钮绑定事件
  31. btn.onclick = function () {
  32. // 1. 创建ajax对象
  33. var xhr = new XMLHttpRequest();
  34. // 2. 监听请求
  35. xhr.onreadystatechange = function () {
  36. if (xhr.readyState === 4 && xhr.status === 200) {
  37. // console.log(xhr.responseText);
  38. var res = JSON.parse(xhr.responseText);
  39. console.log(res);
  40. // 根据后端返回的状态进行判断
  41. switch (res.status) {
  42. case 0:
  43. case 1:
  44. error = res.message;
  45. break;
  46. default:
  47. error = "未知错误";
  48. }
  49. // 将error信息渲染到页面中
  50. var span = document.createElement("span");
  51. span.innerHTML = error;
  52. form.appendChild(span);
  53. }
  54. };
  55. // 3. 初始化请求参数
  56. xhr.open("POST", "test5.php");
  57. // 4. 使用FormData来组织数据,最终仍是以表单数据方式发送
  58. var data = new FormData(form);
  59. // data.append("email", form.email.value);
  60. // data.append("passowrd", form.password.value);
  61. // 5. 发送请求, 发送到服务器上的是json格式字符串
  62. xhr.send(data);
  63. };
  64. </script>
  65. </body>
  66. </html>

5.创建test5.php

  1. <?php
  2. // print_r($_POST);
  3. $users = [
  4. ['id'=>1,'email'=>'melinda@php.cn', 'password'=>'123'],
  5. ['id'=>2,'email'=>'micki@qq.com', 'password'=>'123'],
  6. ];
  7. $status = 0; $message = '<span style="color:red">邮箱或密码错误</span>';
  8. // $status = 0; $message = '邮箱或密码错误';
  9. foreach ($users as $user) {
  10. if ($user['email'] == $_POST['email'] && $user['password'] == $_POST['password']) {
  11. $status = 1;
  12. $message = '<span style="color:green">验证通过</span>';
  13. // $message = '验证通过';
  14. }
  15. }
  16. echo json_encode(['status'=>$status, 'message'=> $message]);
  • 总结POST请求流程:
  • 创建ajax对象: new XMLHttpRequest()
  • 监听请求: onreadystatechange
  • 初始化请求参数: open()
  • 使用FormData来组织数据
  • 发送json格式字符串到服务器上: send()
Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:传统的ajax技术已非常成熟了, 很好用的
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