您的位置:首页 > Web前端 > HTML

HTML表单(Forms)解析

2014-09-12 10:26 211 查看
[导读] HTML表单(Form)是HTML的一个重要部分,主要用于采集和提交用户输入的信息。举个简单的例子,一个让用户输入姓名的HTML表单(Form)。示例代码如下:

HTML表单(Form)是HTML的一个重要部分,主要用于采集和提交用户输入的信息。

举个简单的例子,一个让用户输入姓名的HTML表单(Form)。示例代码如下:
<form action="http://www.php100.com/me.php" method="get"> 请输入你的姓名:<input type="text" name="yourname"> <input type="submit" value="提交"> </form>

view
source

print?

01.
<
html
>

02.
<
head
><
title
>输入用户姓名</
title
></
head
>

03.
<
body
>

04.

05.
<
form
action
=
"http://www.php100.com/a.php"
method
=
"get"
>

06.
请输入你的姓名:

07.
<
input
type
=
"text"
name
=
"yourname"
>

08.
<
input
type
=
"submit"
value
=
"提交"
>

09.
</
form
>

10.

11.
</
body
>

12.
</
html
>


学习HTML表单(Form)最关键要掌握的有三个要点:

表单控件(Form Controls)
Action
Method

先说表单控件(Form Controls),通过HTML表单的各种控件,用户可以输入文字信息,或者从选项中选择,以及做提交的操作。比如上面的例句里,input type="text"就是一个表单控件,表示一个单行输入框。

用户填入表单的信息总是需要程序来进行处理,表单里的action就指明了处理表单信息的文件。比如上面例句里的http://www.php100.com/html/asdocs/html_tutorials/yourname.php。

至于method,表示了发送表单信息的方式。method有两个值:get和post。get的方式是将表单控件的name/value信息经过编码之后,通过URL发送(你可以在地址栏里看到)。而post则将表单的内容通过http发送,你在地址栏看不到表单的提交信息。那什么时候用get,什么时候用post呢?一般是这样来判断的,如果只是为取得和显示数据,用get;一旦涉及数据的保存和更新,那么建议用post。


HTML表单(Form)常用控件(Controls)

HTML表单(Form)常用控件有:

表单控件(Form Contros)说明
input type="text"单行文本输入框
input type="submit"将表单(Form)里的信息提交给表单里action所指向的文件
input type="checkbox"复选框
input type="radio"单选框
select下拉框
textArea多行文本输入框
input type="password"密码输入框(输入的文字用*表示)

表单控件(Form Control):单行文本输入框(input type="text")

单行文本输入框允许用户输入一些简短的单行信息,比如用户姓名。例句如下:
<input type="text" name="yourname">

view
source

print?

01.
<
html
>

02.
<
head
><
title
>输入用户姓名</
title
></
head
>

03.
<
body
>

04.

05.
<
form
action
=
"http://www.php100.com/html/asdocs/html_tutorials/yourname.php"
method
=
"get"
>

06.
请输入你的姓名:

07.
<
input
type
=
"text"
name
=
"yourname"
>

08.
<
input
type
=
"submit"
value
=
"提交"
>

09.
</
form
>

10.

11.
</
body
>

12.
</
html
>


表单控件(Form Control):复选框(input type="checkbox")

复选框允许用户在一组选项里,选择多个。示例代码:
<input type="checkbox" name="fruit" value ="apple">苹果<br> <input type="checkbox" name="fruit" value ="orange">桔子<br> <input type="checkbox" name="fruit" value ="mango">芒果<br>

view
source

print?

01.
<
html
>

02.
<
head
><
title
>选择</
title
></
head
>

03.
<
body
>

04.
请选择你喜欢的水果:<
br
>

05.
<
form
action
=
"http://www.php100.com/html/asdocs/html_tutorials/choose.php"
method
=
"post"
>

06.
<
input
type
=
"checkbox"
name
=
"fruit"
value
=
"apple"
>苹果<
br
>

07.
<
input
type
=
"checkbox"
name
=
"fruit"
value
=
"orange"
>桔子<
br
>

08.
<
input
type
=
"checkbox"
name
=
"fruit"
value
=
"mango"
>芒果<
br
>

09.
<
input
type
=
"submit"
value
=
"提交"
>

10.
</
form
>

11.
</
body
>

12.
</
html
>


用checked表示缺省已选的选项。
<input type="checkbox" name="fruit" value ="orange" checked>桔子<br>

view
source

print?

01.
<
html
>

02.
<
head
><
title
>选择</
title
></
head
>

03.
<
body
>

04.
请选择你喜欢的水果:<
br
>

05.
<
form
action
=
"http://www.php100.com/html/asdocs/html_tutorials/choose.php"
method
=
"post"
>

06.
<
input
type
=
"checkbox"
name
=
"fruit"
value
=
"apple"
>苹果<
br
>

07.
<
input
type
=
"checkbox"
name
=
"fruit"
value
=
"orange"
checked>桔子<
br
>

08.
<
input
type
=
"checkbox"
name
=
"fruit"
value
=
"mango"
>芒果<
br
>

09.
<
input
type
=
"submit"
value
=
"提交"
>

10.
</
form
>

11.
</
body
>

12.
</
html
>


表单控件(Form Control):单选框(input type="radio")

使用单选框,让用户在一组选项里只能选择一个。示例代码:
<input type="radio" name="fruit" value ="Apple">苹果<br> <input type="radio" name="fruit" value ="Orange">桔子<br> <input type="radio" name="fruit" value ="Mango">芒果<br>

view
source

print?

01.
<
html
>

02.
<
head
><
title
>选择</
title
></
head
>

03.
<
body
>

04.
请选择你最喜欢的水果:<
br
>

05.
<
form
action
=
"http://www.php100.com/html/asdocs/html_tutorials/choose.php"
method
=
"post"
>

06.
<
input
type
=
"radio"
name
=
"fruit"
value
=
"Apple"
>苹果<
br
>

07.
<
input
type
=
"radio"
name
=
"fruit"
value
=
"Orange"
>桔子<
br
>

08.
<
input
type
=
"radio"
name
=
"fruit"
value
=
"Mango"
>芒果<
br
>

09.

10.
<
input
type
=
"submit"
value
=
"提交"
>

11.
</
form
>

12.
</
body
>

13.
</
html
>


用checked表示缺省已选的选项。
<input type="radio" name="fruit" value ="Orange" checked>桔子<br>

view
source

print?

01.
<
html
>

02.
<
head
><
title
>选择</
title
></
head
>

03.
<
body
>

04.
请选择你最喜欢的水果:<
br
>

05.
<
form
action
=
"http://www.php100.com/html/asdocs/html_tutorials/choose.php"
method
=
"post"
>

06.
<
input
type
=
"radio"
name
=
"fruit"
value
=
"Apple"
>苹果<
br
>

07.
<
input
type
=
"radio"
name
=
"fruit"
value
=
"Orange"
checked>桔子<
br
>

08.
<
input
type
=
"radio"
name
=
"fruit"
value
=
"Mango"
>芒果<
br
>

09.

10.
<
input
type
=
"submit"
value
=
"提交"
>

11.
</
form
>

12.
</
body
>

13.
</
html
>


表单控件(Form Control):下拉框(select)

下拉框(Select)既可以用做单选,也可以用做复选。单选例句如下:
<select name="fruit" > <option value="apple">苹果 <option value="orange">桔子 <option value="mango">芒果 </select>

view
source

print?

01.
<
html
>

02.
<
body
>

03.
<
form
action
=
"http://www.php100.com/html/asdocs/html_tutorials/choose.php"
method
=
"post"
>

04.
你最喜欢的水果是:

05.
<
select
name
=
"fruit"
>

06.
<
option
value
=
"apple"
>苹果

07.
<
option
value
=
"orange"
>桔子

08.
<
option
value
=
"mango"
>芒果

09.
</
select
>

10.
<
input
type
=
"submit"
value
=
"提交"
>

11.
</
form
>

12.
</
body
>

13.
</
html
>


如果要变成复选,加muiltiple即可。用户用Ctrl来实现多选。例句:
<select name="fruit" multiple>

view
source

print?

01.
<
html
>

02.
<
head
><
title
>选择</
title
></
head
>

03.
<
body
>

04.
<
form
action
=
"http://www.php100.com/html/asdocs/html_tutorials/choose.php"
method
=
"post"
>

05.
你最喜欢的水果是:<
br
>

06.
<
select
name
=
"fruit"
multiple>

07.
<
option
value
=
"apple"
>苹果

08.
<
option
value
=
"orange"
>桔子

09.
<
option
value
=
"mango"
>芒果

10.
</
select
>

11.
<
br
>

12.
<
input
type
=
"submit"
value
=
"提交"
>

13.
</
form
>

14.
</
body
>

15.
</
html
>


用户还可以用size属性来改变下拉框(Select)的大小。

view
source

print?

01.
<
html
>

02.
<
head
><
title
>选择</
title
></
head
>

03.
<
body
>

04.

05.
<
form
action
=
"http://www.php100.com/html/asdocs/html_tutorials/choose.php"
method
=
"post"
>

06.
你最喜欢的水果是:<
br
>

07.
<
select
name
=
"fruit"
size
=
"1"
multiple>

08.
<
option
value
=
"apple"
>苹果

09.
<
option
value
=
"orange"
>桔子

10.
<
option
value
=
"mango"
>芒果

11.
</
select
>

12.
<
input
type
=
"submit"
value
=
"提交"
>

13.
</
form
>

14.
</
body
>

15.
</
html
>


表单控件(Form Control):多行输入框(textarea)

多行输入框(textarea)主要用于输入较长的文本信息。例句如下:
<textarea name="yoursuggest" cols ="50" rows ="3"></textarea>

其中cols表示textarea的宽度,rows表示textarea的高度。

view
source

print?

01.
<
html
>

02.
<
head
><
title
>请提宝贵意见</
title
></
head
>

03.
<
body
>

04.

05.
<
form
action
=
"http://www.php100.com/html/asdocs/html_tutorials/suggest.php"
method
=
"get"
>

06.
请提宝贵意见:<
br
>

07.
<
textarea
name
=
"yoursuggest"
cols
=
"50"
rows
=
"3"
></
textarea
>

08.
<
br
>

09.
<
input
type
=
"submit"
value
=
"提交"
>

10.
</
form
>

11.

12.
</
body
>

13.
</
html
>


表单控件(Form Control):密码输入框(input type="password")

密码输入框(input type="password")主要用于一些保密信息的输入,比如密码。因为用户输入的时候,显示的不是输入的内容,而是黑点符号。。例句如下:
<input type="password" name="yourpw">

view
source

print?

01.
<
html
>

02.
<
head
><
title
>输入用户姓名和密码
</
title
></
head
>

03.
<
body
>

04.

05.
<
form
action
=
"http://www.php100.com/html/asdocs/html_tutorials/userpw.php"
method
=
"post"
>

06.
请输入你的姓名:

07.
<
input
type
=
"text"
name
=
"yourname"
><
br
>

08.
请输入你的密码:

09.
<
input
type
=
"password"
name
=
"yourpw"
><
br
>

10.

11.
<
input
type
=
"submit"
value
=
"提交"
>

12.
</
form
>

13.

14.
</
body
>

15.
</
html
>


表单控件(Form Control):提交(input type="submit")

通过提交(input type=submit)可以将表单(Form)里的信息提交给表单里action所指向的文件。例句如下:
<input type="submit" value="提交">

view
source

print?

01.
<
html
>

02.
<
head
><
title
>输入用户姓名</
title
></
head
>

03.
<
body
>

04.

05.
<
form
action
=
"http://www.php100.com/html/asdocs/html_tutorials/yourname.php"
method
=
"get"
>

06.
请输入你的姓名:

07.
<
input
type
=
"text"
name
=
"yourname"
>

08.
<
input
type
=
"submit"
value
=
"提交"
>

09.
</
form
>

10.

11.
</
body
>

12.
</
html
>


表单控件(Form Control):图片提交(input type="image")

input type=image 相当于 input type=submit,不同的是,input type=image 以一个图片作为表单的提交按钮,其中 src 属性表示图片的路径。
<input type="image" src ="images/icons/go.gif" alt ="提交" NAME="imgsubmit">

view
source

print?

01.
<
html
>

02.
<
head
><
title
>输入用户姓名</
title
></
head
>

03.
<
body
>

04.

05.
<
form
action
=
"http://www.php100.com/html/asdocs/html_tutorials/yourname.php"
method
=
"get"
>

06.
请输入你的姓名:

07.
<
input
type
=
"text"
name
=
"yourname"
><
br
>

08.
<
input
type
=
"image"
src
=
"images/icons/go.gif"

09.
alt
=
"提交"
NAME
=
"imagesubmit"
>

10.
</
form
>

11.

12.
</
body
>

13.
</
html
>


原文地址: http://www.php100.com/html/program/html/2013/0904/1058.htm
l
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: