Blogger Information
Blog 145
fans 7
comment 7
visits 164158
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
01月09日作业:默写php循环语句和表单制作及验证
李东亚¹⁸⁰³⁹⁵⁴⁰¹²⁰
Original
1028 people have browsed it

作业一:php循环语句

1、for(循环变量初始化;循环条件;更新条件) {……}循环实例:

(1)、代码

  1. for ($a=0$a<10 $a++) {
  2. echo $a,'<br>';
  3. }

(2)、效果:

2、while(循环判断条件){……更新条件;……}或者do {……更新条件……}while(循环判断条件);循环实列:
(1)、代码:

  1. $a=0;
  2. while($a<10) {
  3. echo $a,',';
  4. $a++;
  5. }
  6. $a=0;
  7. do {
  8. echo $a,'<br>';
  9. $a++;
  10. } while($a<0);

(2)、效果:

作业二:表单制作及验证

1、表单制作

(1)代码:

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>用户注册表单</title>
  6. <style>
  7. .login{
  8. width: 400px;
  9. height: 430px;
  10. background-color: #55a532;
  11. margin:0 auto;
  12. padding: 10px;
  13. font-size: 16px;
  14. color: #fffdef;
  15. border-radius: 20px;
  16. }
  17. .login:hover {
  18. box-shadow: 0 0 3px #333333 ;
  19. }
  20. .login > h3 {
  21. width: 100px;
  22. margin: 20px auto;
  23. color: #222222;
  24. }
  25. .login > form {
  26. display: grid;
  27. grid-template-columns: 70px 1fr;
  28. grid-template-rows:repeat(7,40px) ;
  29. place-items: center;
  30. }
  31. .login > form > input {
  32. width: 200px;
  33. height: 25px;
  34. }
  35. .login > form > input:nth-last-of-type(n+3):hover {
  36. box-shadow: 0 0 3px #df5000 inset;
  37. }
  38. .login > form > label {
  39. width: 80px;
  40. height: 25px;
  41. position: relative;
  42. left:60px;
  43. }
  44. .login > form > input:nth-last-of-type(-n+2) {
  45. margin: 15px 0;
  46. width:100px;
  47. height:35px;
  48. border-style: none;
  49. background-color: #ff0000;
  50. color:#fffdef;
  51. position: relative;
  52. left:66px;
  53. }
  54. .login > form > input:nth-last-of-type(-n+2):hover{
  55. background-color: #178CEE;
  56. }
  57. </style>
  58. </head>
  59. <body>
  60. <div class="login">
  61. <h3>新用户注册</h3>
  62. <form action="login_verify.php" method="post" name="sign-in">
  63. <label for="username"><span>用户名:</span></label>
  64. <input type="text" name="username" id="username" autofocus placeholder="请输入你的账号" required>
  65. <label for="pwd1"><span>密&nbsp;码:</span></label>
  66. <input type="password" name="pwd1" id="pwd1" placeholder="请输入你的密码" required>
  67. <label for="pwd2"><span>密&nbsp;码:</span></label>
  68. <input type="password" name="pwd2" id="pwd2" placeholder="请再次输入你的密码" required>
  69. <label for="email"><span>邮箱</span></label>
  70. <input type="email" name="email" id="email" placeholder="请输入邮箱" required>
  71. <label for="tel"><span>电话</span></label>
  72. <input type="tel" name="tel" id="tel" placeholder="请输入手机号" required>
  73. <label for="gender"><span>性别</span></label>
  74. <div>
  75. <input type="radio" name="gender" value="1" checked>
  76. <input type="radio" name="gender" value="2">
  77. <input type="radio" name="gender" value="0" id="gender" >保密
  78. </div>
  79. <label for="like"><span>兴趣爱好</span></label>
  80. <div>
  81. <input type="checkbox" name="like[]" value="1">html
  82. <input type="checkbox" name="like[]" value="2">css
  83. <input type="checkbox" name="like[]" value="3" checked id="like">php
  84. </div>
  85. <input type="submit" value="注册">
  86. <input type="reset" value="重置">
  87. </form>
  88. </div>
  89. </body>
  90. </html>

2、效果图

3、表单验证

  1. <?php
  2. //echo '用户名:',$_POST['username'],'<br>';
  3. //echo '密码:',$_POST['pwd1'],'<br>';
  4. //echo '密码:',$_POST['pwd2'],'<br>';
  5. //echo '邮箱:',$_POST['email'],'<br>';
  6. //echo '手机:',$_POST['tel'],'<br>';
  7. //echo '性别:',$_POST['gender'],'<br>';
  8. //echo '兴趣爱好'.print_r($_POST["like"],true),'<br>';
  9. //
  10. //echo '<pre>' . print_r($_POST, true) . '</pre>';
  11. //echo '请求判断类型'.$_SERVER['REQUEST_METHOD'].'<br>';
  12. #验证request类型
  13. if ($_SERVER['REQUEST_METHOD']=='POST') {
  14. if (!empty($_POST['username'])) {
  15. echo $_POST['username'];
  16. };
  17. // $_POST['pwd1']==$_POST['pwd2'] ? $pwd=$_POST['pwd1']: print_r('密码不一致');
  18. if (!empty($_POST['username'])) $username=$_POST['username'];
  19. if (!empty($_POST['email'])) $email=$_POST['email'];
  20. if (!empty($_POST['tel'])) $tel=$_POST['tel'];
  21. if (!empty($_POST['gender'])) $gender=$_POST['gender'];
  22. if (!empty($_POST['like'])) $likes=$_POST['like'];
  23. if ( $_POST['pwd1']==$_POST['pwd2']) {
  24. $pwd=$_POST['pwd1'];
  25. } else {
  26. exit('<script>alert("二次密码不一致");history.back();</script>');
  27. }
  28. $users_date=compact('username','pwd','email','tel','gender','likes');
  29. echo '<pre>'.print_r($users_date,true).'</pre>';
  30. echo '<script>alert("注册成功");</script>';
  31. } else {
  32. exit("请求方式不正确");
  33. };

验证效果:


总结:

1、知识点总结:

(1)、字符串操作函数:strlen();mb_strlen();分别计算字符长度和汉字长度;
trim();ltrim();rtrim();剔除字符串,两端、左、右指定字符,默认空格;
(2)、数组:count();计算数组元素个数; key();``、current();获取数组的当前元素的键和值
(3)、随机返回数值:mt_rand();(range();返回数组);
(4)、指针函数:reset();重置指针到数组开始位置
next(); 指针移动到下一个元素,直至移出数组
prev(); 指针移动到上一个元素,直至移出数组
end(); 指针移动到最后一个元素;

2、表单制作总结:

(1)input类型(type):text\tel\email\radio\checkbox\password\file\hidden\submit\reset\button\number\url\color\date\search\等等
(2)input属性:name\value\placeholder\autofocus\required\disabled\readonly
placeholder:输入框提示内容
autofocus:页面加载时自动获得焦点
required:必填\或必选项
(3)、border-radius:2px;设置边框圆角;box-shadow:0 0 3px 3px #ff0000 inset;设置box阴影
(4)、nth-child(n+2);数字表示起始值,n/-n:自动递增,直至表达式表示的数量结束。

3、表单验证:

(1)、超全局:$_POST();``$_GET();``$_REQUEST();
$_SERVER('REQUEST_METHOD');返回值为大写POST或者GET;
(2)、isset();empty();区别:
empty();判断 空字符串, 0, null, false;
isset();检查请求变量是否设置, 并且值不能为NULL,用在设置请求变量默认值;
(3)、分支语句简化:
1、if(条件)语句;
2、三元运算符:(条件)?语句:语句;语句中不能使用echo输出;
(4)、密码加密方式:md5();32位随机字符串;sha1();40位随机字符串;
(5)js:
弹窗函数:<script>alert("二次密码不一致");</script>
返回上页函数:<script>history.back();</script>

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
Author's latest blog post