Blogger Information
Blog 6
fans 1
comment 0
visits 7216
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
如何使用表单元素实现会员注册功能
努力爬坑的小白程序猿
Original
1556 people have browsed it

在日常工作中,表单会经常被使用,例如制作会员注册页面、会员登录页面、留言板等等,下面我来用一个实例来演示如何使用表单实现用户登录和用户注册的。


1、表单的属性和元素

我在学习表单的时候,有这么个疑问:现在我们见到的表单多种多样,有提交信息的,有填写留言的,那我们再制作表单的过程中用到的属性是否是多种的呢?答案是否定的,表单的元素仅有一个,他就是<form>,下面来介绍一下<form>的常用属性:

序号 属性 描述
1 action 表单提交的 URL 地址(处理表单请求的脚本)
2 method 表单提交类型:GET/POST
3 enctype 设置表单提交数据的编码方式
4 name 表单唯一名称,与 ID 同义
5 target 打开请求 URL 的方式,如果_blank

2、常见的表单控件以及常用属性和类型

在表单的制作中,我们最常用到的表单空间元素是<input>,而且为了满足表单在不同的使用场景下的应用,表单的常用属性有以下这些属性:

序号 属性 描述
1 type 控件类型,如文本框, 复选框…
2 name 请求参数的名称,对应于脚本处理的变量名
3 value 请求参数的值,对应于脚本处理的变量值,使用预定义值控件无效
4 form 控件所属表单
5 width 仅限图像域images, 图像宽度 `
6 list 仅限输入框textpassword,下拉框智能提示
7 autocomplate 仅限输入框textpassword,自动完成(通过读取历史记录)
8 maxlength 仅限输入框text/password, 允许输入最大字符数量
9 checked 仅限单选框radio, 复选框checkbox
10 readonly 元素只读,但通过 JavaScript 可修改
11 disabled 元素禁用
12 autofocus 自动获取焦点,一个表单仅限一个控件
13 src 仅限图像域images, 图像 URL 地址
14 placeholder 仅限输入框textpassword,输入框的提示信息
15 height 仅限图像域images, 图像高度

使用以上控件属性可以满足我们大部分表单的使用场景。

3、案例

下面我用以上的元素及控件做个简单的小表单,供大家参考:
效果如下:

下面奉上代码,可供参考:

  1. <div class="from">
  2. <h1>注册表单练习</h1>
  3. <form action="" action="check.php" method="post">
  4. <label for="name"> 用户名:</label
  5. ><input type="text" name="name" id="name" required /><br />
  6. <label for="mima"> 密码:</label
  7. ><input type="password" id="mima" required /><br />
  8. <label for="queren"> 确认密码:</label
  9. ><input type="password" id="queren" required /><br />
  10. <label for="email">邮箱: </label
  11. ><input
  12. type="email"
  13. id="email"
  14. required
  15. title="example@domain.com"
  16. pattern="\w+@[a-z0-9]+\.[a-z]+"
  17. required
  18. title="example@domain.com"
  19. /><br />
  20. <div class="aihao">
  21. <label for="">爱好:</label
  22. ><input type="radio" name="aihao" id="ktv" /><label for="ktv"
  23. >唱歌</label
  24. ><input type="radio" name="aihao" id="swimming" /><label
  25. for="swimming"
  26. >游泳</label
  27. ><input type="radio" name="aihao" id="game" /><label for="game"
  28. >打游戏</label
  29. ><input type="radio" name="aihao" id="play" /><label for="play"
  30. >旅游</label
  31. >
  32. </div>
  33. <div class="qita">
  34. <label for="eud">学历:</label>
  35. <select name="edu" id="edu">
  36. <option value="1">初中</option>
  37. <option value="2">高中</option>
  38. <option value="3">专科</option>
  39. <option value="4">本科</option>
  40. <option value="5">硕士</option>
  41. </select>
  42. <div>
  43. <label for="user-pic">头像:</label>
  44. <input type="hidden" name="MAX_FILE_SIZE" value="80000" />
  45. <input type="file" name="user_pic" id="user-pic" />
  46. </div>
  47. <label for="user-resume">简历:</label>
  48. <div>
  49. <textarea name="user-resume" id="1" cols="30" rows="10"></textarea>
  50. </div>
  51. </div>
  52. <button>提交</button>
  53. </form>
  54. </div>

以上是表单的代码,想达到这个效果,还需要一些css的效果(由于css刚开始学没几天写的很差,大家看看就行),下面奉上css的代码:

  1. <style>
  2. body {
  3. background: url(https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1603708949815&di=a8b4936fd1e4f921b37e0e6b7c726a2c&imgtype=0&src=http%3A%2F%2Fa.hiphotos.baidu.com%2Fzhidao%2Fpic%2Fitem%2F10dfa9ec8a136327faed6472918fa0ec08fac76b.jpg);
  4. background-size: 100%;
  5. overflow: auto;
  6. }
  7. .from {
  8. background-color: rgba(0, 132, 255, 0.6);
  9. width: 500px;
  10. margin: auto;
  11. height: 30em;
  12. margin-top: 10%;
  13. }
  14. h1 {
  15. color: #fff;
  16. text-align: center;
  17. }
  18. form {
  19. text-align: right;
  20. color: #fff;
  21. width: 358px;
  22. font-size: 1.2em;
  23. }
  24. input {
  25. border-radius: 0.4em;
  26. /* width: 205px; */
  27. }
  28. button {
  29. background-color: hotpink;
  30. color: #fff;
  31. cursor: pointer;
  32. border: none;
  33. font-size: 0.8em;
  34. float: right;
  35. margin: 1.5em -8em;
  36. }
  37. .aihao {
  38. width: 444px;
  39. margin: auto;
  40. }
  41. .qita {
  42. text-align: left;
  43. width: 302px;
  44. padding-left: 7em;
  45. }
  46. </style>

4、总结

表单在前端开发里还是比较重要的,还是得多看多写多练,我在写表单的时候,说实话没有踩什么坑,主要还是对一些属性和控件记得不是很牢靠,有些想不起来的地方还是得看资料,还是需要下来多练习练习,争取不看资料自己能熟练的默出来。

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:看来你是有一定的基础, 恭喜你, 一定能成功
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!