博客列表 >登录表单的创建与验证(0108php开发作业)

登录表单的创建与验证(0108php开发作业)

小辰
小辰原创
2020年01月13日 00:13:44667浏览

1.献上老夫的手写图

2.下面是我对于循环的使用与测试代码

效果图

老夫的代码,你值得尝试

  1. <?php
  2. // 建立数组
  3. $kemu = ['music','math','english'];
  4. // 用for循环遍历数组
  5. for ($i=0; $i <count($kemu) ; $i++) {
  6. // echo "{$i}={$kemu[$i]}<br/>";
  7. echo $i.'==>'.$kemu[$i].'<br>';
  8. }
  9. // 用while循环遍历数组
  10. echo "<hr>";
  11. $a=0;
  12. while ( $a< count($kemu)) {
  13. echo "{$a}=={$kemu[$a]}<br/>";
  14. $a++;
  15. }
  16. // 用do..while循环遍历数组
  17. echo "<hr>";
  18. $b=0;
  19. do {
  20. echo $b.'==>'.$kemu[$b].'<br>';
  21. $b++;
  22. }while ($b<count($kemu)) ;
  23. // 用foreach遍历数组
  24. echo "<hr>";
  25. $arr2 = ['id'=>123, 'iphpone'=>'Apple', 'book'=>'jibook', 'time'=>12];
  26. foreach ($arr2 as $key => $value) {
  27. echo "$key => $value <br>";
  28. }
  29. echo '<hr>';
  30. //数组指针的使用
  31. next($kemu);
  32. echo key($kemu) .'---->' . current($kemu) . '<br>';
  33. echo '<hr>';
  34. // 向前移动一位及复位
  35. prev($kemu);
  36. echo key($kemu) .'---->' . current($kemu) . '<br>';
  37. reset($kemu);
  38. echo key($kemu) .'---->' . current($kemu) . '<br>';
  39. // 返回当前所在位的值
  40. var_dump(current($kemu));
  41. echo '<hr>';
  42. // 数组指针遍历函数
  43. while (current($arr2)) {
  44. echo key($arr2) .'---->' . current($arr2) . '<br>';
  45. next($arr2);
  46. }
  47. echo "<hr>";
  48. // 循环表单的使用
  49. $years = range(2000,2020);
  50. $select = '年份:<select name="year">';
  51. foreach ($years as $year) {
  52. $select .='<option value=". $year .">'. $year .'年</option>';
  53. }
  54. $select .= '</select>';
  55. echo $select;
  56. $months = range(1, 12);
  57. $select = '月份: <select name="month">';
  58. foreach ($months as $month) {
  59. $select .= '<option value="' .$month . '">' . $month . '月</option>';
  60. }
  61. $select .= '</select>';
  62. echo $select;
  63. $days = range(1, 31);
  64. $select = '日: <select name="day">';
  65. foreach ($days as $day) {
  66. $select .= "<option value='${day}'>${day}号</option>";
  67. }
  68. $select .= '</select>';
  69. echo $select;

3.下面是我对表单的创建与验证

效果图


要想生活过得去,必须生活多点绿,感觉绿色不错

表单的html代码

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>用户登录</title>
  6. <style>
  7. /*总体样式设计*/
  8. .register {
  9. width: 300px;
  10. display: flex;
  11. flex-direction: column;
  12. color: #555;
  13. font-size: 14px;
  14. margin: auto;
  15. }
  16. .register>h2 {
  17. color:lightblue;
  18. align-self: center;
  19. font-weight: normal;
  20. font-size: 20px;
  21. }
  22. /* 表单样式 */
  23. .register>form {
  24. display: flex;
  25. flex-direction: column;
  26. padding: 20px;
  27. background-color: lightgreen;
  28. border-radius: 70px;
  29. }
  30. .register>form:hover {
  31. box-shadow: 0 0 20px #888;
  32. }
  33. /* 表单中所有inpu控件样式 */
  34. .register>form input {
  35. border: none;
  36. outline: none;
  37. border-radius: 5px;
  38. padding-left: 5px;
  39. margin-left: 2px;
  40. }
  41. .register>form input:hover {
  42. background-color: lightgoldenrodyellow;
  43. }
  44. .register>form>span {
  45. margin: 5px 0;
  46. display: flex;
  47. justify-content: space-between;
  48. }
  49. .register>form> span>label {
  50. font-size: 18px;
  51. }
  52. /* 对按钮所在元素进行设置 */
  53. .register>form>span:last-of-type {
  54. display: flex;
  55. justify-content: center;
  56. }
  57. /* 提交按钮样式 */
  58. .register>form>span:last-of-type > button>a {
  59. color: white;
  60. font-size: 16px;
  61. text-decoration: none;
  62. }
  63. .register>form>span:last-of-type > button {
  64. margin-top: 5px;
  65. margin-left: 15px;
  66. background-color: #888;
  67. border: none;
  68. background-color: lightcoral;
  69. color: white;
  70. width: 76px;
  71. height: 30px;
  72. font-size: 16px;
  73. }
  74. .register>form>span:last-of-type > button:hover {
  75. background-color: blue;
  76. cursor: pointer;
  77. }
  78. .register>form>span:last-of-type > a> button:hover {
  79. background-color: blue;
  80. cursor: pointer;
  81. }
  82. </style>
  83. </head>
  84. <body>
  85. <div class="register">
  86. <h2>用户登录</h2>
  87. <!-- 使用post请求,隐藏传输过程中的信息 -->
  88. <form action="mydemo3.php" method="post">
  89. <span>
  90. <label for="username">用户名:</label>
  91. <input type="text" name="username" id="username" placeholder="不超过10个字符" required autofocus>
  92. </span>
  93. <span>
  94. <label for="email">邮箱:</label>
  95. <input type="email" name="email" id="email" placeholder="不能为空" required>
  96. </span>
  97. <span>
  98. <label for="password1">密码:</label>
  99. <input type="password" name="password1" id="password1" placeholder="不能为空" required>
  100. </span>
  101. <span>
  102. <label for="password2">重复密码:</label>
  103. <input type="password" name="password2" id="password2" placeholder="必须与上面一致" required>
  104. </span>
  105. <span>
  106. <button > <a href="demo2.php">注册</a></button>
  107. <button >登录</button>
  108. </span>
  109. </form>
  110. </div>
  111. </body>
  112. </html>

下面是用于验证的代码

  1. <?php
  2. // echo '请求类型: ' . $_SERVER['REQUEST_METHOD'] . '<br>';
  3. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  4. // 检查是否为空
  5. if (!empty($_POST['username'])) $username = $_POST['username'];
  6. if (!empty($_POST['password1'])) $password1 = $_POST['password1'];
  7. if (!empty($_POST['password2'])) $password2 = $_POST['password2'];
  8. if (!empty($_POST['email'])) $email = $_POST['email'];
  9. // 检查密码是否一致
  10. if ($password1 === $password2) {
  11. $password = md5(sha1($password1));
  12. } else {
  13. exit('<script>alert("二次密码不一致");history.back();</script>');
  14. }
  15. $data = compact('username', 'password', 'email');
  16. echo '<pre>' . print_r($data, true) . '</pre>';
  17. } else {
  18. exit('<h3 style="color:red">请求类型错误!请返回确认!</h3>');
  19. }

4.小结

对于循环遍历数组的使用,我觉得多用用就熟悉了。对于表单的验证,按照步骤来不会出错,值得我注意的是表单主页代码的<form action="mydemo3.php" method="post">两个属性要根据实际需求来设置。

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