博客列表 >用户登录表单数据的前端处理与后端PHP处理流程(1125)

用户登录表单数据的前端处理与后端PHP处理流程(1125)

A 管志岗-电商策划
A 管志岗-电商策划原创
2022年12月04日 17:50:04438浏览

用户登录表单数据的前端处理与后端PHP处理流程

前端代码

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <title>首页</title>
  8. <link rel="stylesheet" href="static/css/style.css" />
  9. </head>
  10. <body>
  11. <?php
  12. require __DIR__ . '/config/common.php';
  13. ?>
  14. <!-- 动态替换首页导航 -->
  15. <header>
  16. <!-- 动态替换首页导航 -->
  17. <nav>
  18. <?php
  19. // $navs = include './data/header.php';
  20. $navs = include TMPL_PATH_PUBLIC . '/header.php';
  21. ?>
  22. <?php foreach ($navs as $nav) : ?>
  23. <a href="<?= $nav['url'] ?>"><?= $nav['title'] ?></a>
  24. <?php endforeach ?>
  25. </nav>
  26. </header>
  27. <!-- 主体 -->
  28. <main>
  29. <!-- 用户登录 -->
  30. <form class="login" id="login.php">
  31. <table>
  32. <caption>
  33. 用户登录
  34. </caption>
  35. <tbody>
  36. <tr>
  37. <td><label for="email">邮箱:</label></td>
  38. <td><input type="email" name="email" id="email" value="" /></td>
  39. </tr>
  40. <tr>
  41. <td><label for="password">密码:</label></td>
  42. <td><input type="password" name="password" id="password" /></td>
  43. </tr>
  44. <tr>
  45. <td colspan="2"><button type="button" onclick="check(this)">提交</button></td>
  46. </tr>
  47. </tbody>
  48. </table>
  49. </form>
  50. <p>
  51. <a href="register.html">没有帐号,请先注册</a>
  52. </p>
  53. </main>
  54. <?php
  55. include TMPL_PATH_PUBLIC . '/footer.php'
  56. ?>
  57. <script>
  58. async function check(btn) {
  59. const email = btn.form.email.value.trim();
  60. const password = btn.form.password.value.trim();
  61. // 非空验证
  62. if (email.length > 0 && password.length > 0) {
  63. const response = await fetch('./lib/user/login.php', {
  64. // 请求类型
  65. method: 'POST',
  66. // 请求头
  67. headers: {
  68. 'Content-Type': 'application/json; charset = utf-8',
  69. },
  70. body: JSON.stringify({
  71. email,
  72. password
  73. })
  74. });
  75. // 2. 解析数据
  76. const data = await response.json();
  77. console.log(data);
  78. } else {
  79. alert('邮箱或密码不能为空');
  80. return false;
  81. }
  82. }
  83. </script>
  84. </body>
  85. </html>

后端代码

  1. <?php
  2. // php接收原始数据
  3. $json = file_get_contents('php://input');
  4. // json 转成php可以处理的数据,这里是数组,也可以是对象,不加true就是对象
  5. $user = json_decode($json, true);
  6. // 验证
  7. // 返回数据
  8. echo json_encode($user);

逻辑图
逻辑图

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议