search
HomeWeb Front-endHTML Tutorial了解HTML表单之form元素_html/css_WEB-ITnose

目录 [1]表单名称 [2]字符集 [3]提交地址 [4]打开方式 [5]数据编码 [6]数据发送 [7]自动完成 [8]表单验证

前面的话

  表单是网页与用户的交互工具,由一个

元素作为容器构成,封装其他任何数量的表单控件,还有其他任何元素里可用的标签

  表单能够包含

  [注意]表单里嵌套表单是不允许的

 

form元素

  form元素有accept-charset、action、autocomplete、enctype、method、name、novalidate、target共8个属性,其中action和name属性为必需项

表单名称

  name属性规定表单名称,如果name="test",则Javascript可以使用document.forms.test来获取该表单

<form method="get" action="form.php" name="test"></form>    <script>    var oForm = document.forms.test;    console.log(oForm.method);//get</script>

 

字符集

  accept-charset属性规定服务器用哪种字符集处理表单数据,通常不指定,那么页面的字符编码会被使用

 

提交地址

  action属性规定提交表单时,向何处发送表单数据;如果忽略这个属性,表单会重定向到表单所在的URL

 

打开方式

  target属性规定在何处打开action URL。共5个值_blank、_self、_parent、_top、framename。

  关于target属性的使用移步至此

 

数据编码

  enctype 属性规定在发送到服务器之前应该如何对表单数据进行编码。大多数情况下该属性不需要设置

  application/x-www-form-urlencoded   在发送前编码所有字符(默认)

  multipart/form-data            不对字符编码。在使用包含文件上传控件的表单时,必须使用该值

  text/plain                   空格转换为 "+" 加号,但不对特殊字符编码

 

数据发送

  表单可以用两种方式发送数据:GET和POST,默认为GET方法。

POST方法
  如果采用POST方法,浏览器将会按照下面两步来发送数据。首先,浏览器将与action属性中指定的表单处理服务器建立联系,一旦建立连接之后,浏览器就会按分段传输的方法将数据发送给服务器。

  在服务器端,一旦POST样式的应用程序开始执行时,就应该从一个标志位置读取参数,而一旦读到参数,在应用程序能够使用这些表单值以前,必须对这些参数进行解码。用户特定的服务器会明确指定应用程序应该如何接受这些参数。

【应用场景】

  [1]大数据处理,因为POST方法相比GET方法而言,处理更多字段

  [2]安全数据,因为GET 方法将表单参数直接放在应用程序的 URL 中,这样网络窥探者可以很轻松地捕获它们,还可以从服务器的日志文件中进行摘录;而POST方法则没有这方面的漏洞

GET方法

  如果采用GET方法,浏览器会与表单处理服务器建立连接,然后直接在一个传输步骤中发送所有的表单数据:浏览器会将数据直接附在表单的action URL之后。这两者之间用问号进行分隔。

【应用场景】

  [1]获得最佳表单传输性能,因为GET发送只有少数简单字段

  [2]简单处理,因为GET方法无需处理编码解码方法

  [3]传参处理,因为GET方法允许把表单的参数包括进来作为 URL 的一部分

<h3 id="get方法">get方法</h3><form method="get" action="form.php" target = "_blank">    <p><label>x:<input name="x"></label></p>    <p><label>y:<input name="y"></label></p>    <p><button type="submit">Submit</button></p></form>    <a title="form.php?x=28&y=66" href="form.php?x=28&y=66">a标签传参</a><h3 id="post方法">post方法</h3><form method="post" action="form.php"  target = "_blank">    <p><label>x:<input name="x"></label></p>    <p><label>y:<input name="y"></label></p>    <p><button type="submit">Submit</button></p></form>    

//GET方法的URL显示为: http://127.0.0.1/form.php?x=1&y=2//POST方法的URL显示为:http://127.0.0.1/form.php<p><?phpif(isset($_REQUEST["x"]) && isset($_REQUEST["y"])){    echo "x: " .$_REQUEST["x"] ."<br>";    echo "y: " .$_REQUEST["y"];}?>    </p>

 

自动完成

  autocomplete属性规定表单是否应该启用自动完成功能。当用户在字段开始键入时,浏览器基于之前键入过的值,应该显示出在字段中填写的选项

<form autocomplete="on | off">        该属性默认为on,当设置为off时,规定禁用自动完成功能<button id="btn1">打开自动完成</button><button id="btn2">关闭自动完成</button><form method="get" action="#" name="test">    <p><label>x:<input name="x"></label></p>    <p><label>y:<input name="y"></label></p>    <p><button type="submit">Submit</button></p>    </form>    <script>var oForm = document.forms.test;btn1.onclick = function(){    oForm.autocomplete = 'on';};btn2.onclick = function(){    oForm.autocomplete = 'off';};</script>

 

表单验证

  novalidate属性规定当提交表单时不对其进行验证

<button id="btn1">打开验证</button><button id="btn2">关闭验证</button><form method="get" action="#" name="test">    E-mail: <input type="email" name="user_email" />    <input type="submit" /></form>    <script>var oForm = document.forms.test;btn1.onclick = function(){    oForm.removeAttribute('novalidate');};btn2.onclick = function(){    oForm.setAttribute('novalidate','');};</script>

 

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
HTML: Is It a Programming Language or Something Else?HTML: Is It a Programming Language or Something Else?Apr 15, 2025 am 12:13 AM

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

HTML: Building the Structure of Web PagesHTML: Building the Structure of Web PagesApr 14, 2025 am 12:14 AM

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

From Text to Websites: The Power of HTMLFrom Text to Websites: The Power of HTMLApr 13, 2025 am 12:07 AM

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.

Understanding HTML, CSS, and JavaScript: A Beginner's GuideUnderstanding HTML, CSS, and JavaScript: A Beginner's GuideApr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

The Role of HTML: Structuring Web ContentThe Role of HTML: Structuring Web ContentApr 11, 2025 am 12:12 AM

The role of HTML is to define the structure and content of a web page through tags and attributes. 1. HTML organizes content through tags such as , making it easy to read and understand. 2. Use semantic tags such as, etc. to enhance accessibility and SEO. 3. Optimizing HTML code can improve web page loading speed and user experience.

HTML and Code: A Closer Look at the TerminologyHTML and Code: A Closer Look at the TerminologyApr 10, 2025 am 09:28 AM

HTMLisaspecifictypeofcodefocusedonstructuringwebcontent,while"code"broadlyincludeslanguageslikeJavaScriptandPythonforfunctionality.1)HTMLdefineswebpagestructureusingtags.2)"Code"encompassesawiderrangeoflanguagesforlogicandinteract

HTML, CSS, and JavaScript: Essential Tools for Web DevelopersHTML, CSS, and JavaScript: Essential Tools for Web DevelopersApr 09, 2025 am 12:12 AM

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

The Roles of HTML, CSS, and JavaScript: Core ResponsibilitiesThe Roles of HTML, CSS, and JavaScript: Core ResponsibilitiesApr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools