search
HomeWeb Front-endHTML TutorialAnalyze the differences between input submit, button and enter key to submit data

Recently, a lot of form submissions have been used in projects, and I found that input, button, submit and even the enter key can trigger form submission. The following will tell you about the differences in their use.


<form>
  <input name="name">
  <input type="submit" value="提交">
</form>

Submitted in this way, when the input value is 22222222, the url submitted later will become localhost:3980/input.html?name=222222
There are some details worth noting:

After setting type=submit, the input control will become a button, and the displayed text will be its value. The default value is Submit.
The default value of form[method] is GET, so the GET method will be used to jump to the page after submission.
The default value of input[type] is text, so the first input is displayed as a text box.

Input is actually a button modified from an input control, which originated from the crude design of the early days of the Web. We can verify this by setting a name to it:

<input name="btn" value="Submit" type="submit">

The URL after submission will change to localhost:3980/input.html?name=222222&btn=Submit

Note that the URL is /?key=foo&btn=ok. The input control as a button is also submitted to the server as a form input. Is it an interactive control or a data control? The positioning is a bit unclear. In addition, its style is difficult to customize and cannot be used as a container for other tags, so it is recommended not to use input as a form submission button.

Note: The type attribute of input can also be button. At this time, it is just a button and will not trigger form submission.

2. button[tpe=submit]The semantics of button are very clear, that is, a button does not contain data, and its function is user interaction. But it also has type and value attributes. The default value of type is submit, so clicking a button will cause the form to be submitted:


<form>
 <input name=&#39;key&#39;>
 <button>确定</button>
</form>

If you are doing IE browser compatibility, please remember button[ The default value of type] in IE is button, which means it is just a button and does not trigger a form submission.

In addition, we specify the text of the button by setting the element content. This means that the button is a container control, which can contain arbitrary HTML tags, and the style is easier to customize. This is one of the reasons why buttons are used extensively as examples in the Bootstrap documentation.

However, buttons will be messy. Button can set name and value. When the form is submitted, the value will be submitted to the server as form data. In IE, the content between the button's start and end tags will even be submitted to the server as the value corresponding to the name. The similarities between button and input don't stop there. Button can also be set with type=reset. Clicking the button will cause the form to be reset (this is quite useful). w3school gives the following example:


<form action="form_action.asp" method="get">
 First name: <input type="text" name="fname" />
 Last name: <input type="text" name="lname" />
 <button type="submit" value="Submit">Submit</button>
 <button type="reset" value="Reset">Reset</button>
</form>

I won’t say much about buttons. It is recommended to use buttons as interactive buttons to submit forms. Please also pay attention to setting type=submit to be compatible with IE.

Enter key to submit the form

The Enter key can submit the form! But you may have noticed that not all forms can be submitted with the Enter key. Let’s look at the HTML2.0 standard:

When there is only one single-line text input field in a form, the user agent should accept Enter in that field as a request to submit the form.

When there is only a single-line text input control in the form, the user agent should accept the Enter key to submit the form.

"Single line" refers to the type being text rather than textarea. Obviously, it is unacceptable to press enter to submit the form in textarea. In fact, in practice, multiple single-line inputs can also be submitted using Enter, such as the login page.

4. Preventing form submission

Preventing form submission is also a common topic and is usually used for client-side form validation. The general method is to set onsubmit:


<form onsubmit="return false;">
 <input name=&#39;key&#39;>
 <input value=&#39;ok&#39; type=&#39;submit&#39;>
</form>

You only need to return false at the end of a series of onsubmit statements to prevent it from submitting. If you wish to call a method to decide whether to block a commit, remember to return the method's return value here:


<form onsubmit="return false;">
 <input name=&#39;key&#39;>
 <input value=&#39;ok&#39; type=&#39;submit&#39;>
</form>

The above is the detailed content of Analyze the differences between input submit, button and enter key to submit data. For more information, please follow other related articles on the PHP Chinese website!

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
How do you set the lang attribute on the  tag? Why is this important?How do you set the lang attribute on the tag? Why is this important?May 08, 2025 am 12:03 AM

Setting the lang attributes of a tag is a key step in optimizing web accessibility and SEO. 1) Set the lang attribute in the tag, such as. 2) In multilingual content, set lang attributes for different language parts, such as. 3) Use language codes that comply with ISO639-1 standards, such as "en", "fr", "zh", etc. Correctly setting the lang attribute can improve the accessibility of web pages and search engine rankings.

What is the purpose of HTML attributes?What is the purpose of HTML attributes?May 07, 2025 am 12:01 AM

HTMLattributesareessentialforenhancingwebelements'functionalityandappearance.Theyaddinformationtodefinebehavior,appearance,andinteraction,makingwebsitesinteractive,responsive,andvisuallyappealing.Attributeslikesrc,href,class,type,anddisabledtransform

How do you create a list in HTML?How do you create a list in HTML?May 06, 2025 am 12:01 AM

TocreatealistinHTML,useforunorderedlistsandfororderedlists:1)Forunorderedlists,wrapitemsinanduseforeachitem,renderingasabulletedlist.2)Fororderedlists,useandfornumberedlists,customizablewiththetypeattributefordifferentnumberingstyles.

HTML in Action: Examples of Website StructureHTML in Action: Examples of Website StructureMay 05, 2025 am 12:03 AM

HTML is used to build websites with clear structure. 1) Use tags such as, and define the website structure. 2) Examples show the structure of blogs and e-commerce websites. 3) Avoid common mistakes such as incorrect label nesting. 4) Optimize performance by reducing HTTP requests and using semantic tags.

How do you insert an image into an HTML page?How do you insert an image into an HTML page?May 04, 2025 am 12:02 AM

ToinsertanimageintoanHTMLpage,usethetagwithsrcandaltattributes.1)UsealttextforaccessibilityandSEO.2)Implementsrcsetforresponsiveimages.3)Applylazyloadingwithloading="lazy"tooptimizeperformance.4)OptimizeimagesusingtoolslikeImageOptimtoreduc

HTML's Purpose: Enabling Web Browsers to Display ContentHTML's Purpose: Enabling Web Browsers to Display ContentMay 03, 2025 am 12:03 AM

The core purpose of HTML is to enable the browser to understand and display web content. 1. HTML defines the web page structure and content through tags, such as, to, etc. 2. HTML5 enhances multimedia support and introduces and tags. 3.HTML provides form elements to support user interaction. 4. Optimizing HTML code can improve web page performance, such as reducing HTTP requests and compressing HTML.

Why are HTML tags important for web development?Why are HTML tags important for web development?May 02, 2025 am 12:03 AM

HTMLtagsareessentialforwebdevelopmentastheystructureandenhancewebpages.1)Theydefinelayout,semantics,andinteractivity.2)SemantictagsimproveaccessibilityandSEO.3)Properuseoftagscanoptimizeperformanceandensurecross-browsercompatibility.

Explain the importance of using consistent coding style for HTML tags and attributes.Explain the importance of using consistent coding style for HTML tags and attributes.May 01, 2025 am 12:01 AM

A consistent HTML encoding style is important because it improves the readability, maintainability and efficiency of the code. 1) Use lowercase tags and attributes, 2) Keep consistent indentation, 3) Select and stick to single or double quotes, 4) Avoid mixing different styles in projects, 5) Use automation tools such as Prettier or ESLint to ensure consistency in styles.

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool