Blogger Information
Blog 32
fans 2
comment 2
visits 23324
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php循环控制和表单验证(0108)
暴风战斧
Original
539 people have browsed it

编程思路

1、for和while循环练习我首先是把函数语法看了一遍,然后又查了一遍资料,通过模仿老师遍历数组的方式完成练习;
2、表单的验证,因为跟注册不同,登录没有两次密码互相校验,我设计成了密码的校验,如果密码错误则提示,正确则通过!

作业总结

1、for和while函数单看语法真的超级简单,但是用来遍历数组时就感觉到了,不是简单的按语法写完,其中循环条件有点绕需要多理解。
2、表单前端代码多花了点时间,因为想做的好看一点点,表单验证代码写的比较快,就写下验证密码是否正确,以前写密码的input时总是忘记把type设置成password,这次没有忘记,还给代码做了加密!

1.for、while循环控制和遍历关联数组

  • 演示代码
  1. <?php
  2. // 1、for、while循环控制
  3. $arr0 = ['a','b','c','d'];
  4. // for循环
  5. $result = '';
  6. for ($i = 0; $i < count($arr0); $i++) {
  7. $result .= '<span">' . $arr0[$i] . '</span>,' ;
  8. }
  9. echo rtrim($result, ',') . '<br>';
  10. // while循环
  11. $i = 0;
  12. $result = '';
  13. while ($i < count($arr0)) {
  14. $result .= '<span">' . $arr0[$i] . '</span>,' ;
  15. $i++;
  16. }
  17. echo rtrim($result, ',') . '<br>';
  18. // do...while循环
  19. $i = 0;
  20. $result = '';
  21. do {
  22. $result .= '<span">' . $arr0[$i] . '</span>,' ;
  23. $i++;
  24. }
  25. while ($i < count($arr0));
  26. echo rtrim($result, ',') . '<br>';
  27. echo '<hr>';
  28. // 2、for、while遍历关联数组
  29. $arr1 = ['id'=>1001, 'name'=>'小明', 'email'=>'admin@qq.com', 'school'=>'瑞昌二中'];
  30. // for遍历关联数组
  31. for ($i = 0; $i < count($arr1); $i++) {
  32. echo key($arr1). '=>'. current($arr1). '<br>';
  33. next($arr1);
  34. }
  35. echo '<hr>';
  36. // while遍历关联数组
  37. reset($arr1);
  38. while (key($arr1)) {
  39. echo key($arr1). '=>'. current($arr1). '<br>';
  40. next($arr1);
  41. }
  42. ?>
  • 效果图

2、表单登录验证

  • 登录表单代码
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>用户登录</title>
  6. <style>
  7. .container {
  8. display: flex;
  9. width: 300px;
  10. height: 200px;
  11. background-color: #333333;
  12. border-radius: 10px;
  13. margin: 30px auto;
  14. flex-direction: column;
  15. justify-content: space-between;
  16. align-items: center;
  17. }
  18. .container > h3 {
  19. border-bottom: 2px solid white;
  20. margin: 20px 0 0 0;
  21. color: white;
  22. }
  23. /*设置表单样式*/
  24. .container > form {
  25. display: flex;
  26. flex-direction: column;
  27. margin: 0;
  28. padding: 10px;
  29. }
  30. .container > form input {
  31. border: none;
  32. width: 160px;
  33. }
  34. .container > form input:hover {
  35. box-shadow: 0 0 8px #888888;
  36. }
  37. .container > form label {
  38. color: white;
  39. }
  40. .container > form > span {
  41. margin: 10px 0;
  42. display: flex;
  43. justify-content: space-between;
  44. }
  45. .container > form > span:last-of-type {
  46. display: flex;
  47. justify-content: center;
  48. }
  49. /*按钮样式设置*/
  50. .container > form > span:last-of-type > button {
  51. border: none;
  52. background-color: teal;
  53. color: white;
  54. font-size: 14px;
  55. width: 120px;
  56. height: 24px;
  57. }
  58. .container > form > span:last-of-type > button:hover {
  59. background-color: lightseagreen;
  60. cursor: pointer;
  61. }
  62. </style>
  63. </head>
  64. <body>
  65. <div class="container">
  66. <h3>用户登录</h3>
  67. <form action="check.php" method="post">
  68. <span>
  69. <label for="username">用户名:</label>
  70. <input type="text" name="username" id="username" placeholder=" 请输入用户名" required autofocus>
  71. </span>
  72. <span>
  73. <label for="password">密码:</label>
  74. <input type="password" name="password" id="password" placeholder=" 请输入密码" required autofocus>
  75. </span>
  76. <span>
  77. <button>立即登录</button>
  78. </span>
  79. </form>
  80. </div>
  81. </body>
  82. </html>
  • 表单验证代码
  1. <?php
  2. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  3. if (!empty($_POST['username'])) $username = $_POST['username'];
  4. if (!empty($_POST['password'])) $password = $_POST['password'];
  5. //二次密码必须一致
  6. if (!empty($_POST['password']) & $password === '123456') {
  7. $password = sha1($password);
  8. } else {
  9. exit('<script>alert("密码错误,请确认后登录!");history.back();</script>');
  10. }
  11. $login_data = compact('username','password');
  12. echo '<pre>' . print_r($login_data,true) . '</pre>';
  13. } else {
  14. exit('<h3 style="color:red">请求类型错误!</h3>');
  15. }
  16. ?>
  • 效果图
  • 密码正确:

  • 密码错误:

3、手写for、while、数组指针函数语法

Correcting teacher:天蓬老师天蓬老师

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