


Example analysis of the difference between input, submit, button and enter key to submit data
This article will use examples to analyze the differences between input, submit, button and the Enter key to submit data. Recently, a lot of form submissions have been used in projects, and it is found that input, button, submit and even the Enter key can trigger form submission. The following will be explained separately. Let's talk about the differences between them in use, I hope it can help everyone.
<form> <input name="name"> <input type="submit" value="提交"> </form>
Submit in this way, when the input value is 22222222, the url submitted later will become localhost:3980/input.html?name=222222
There are some noteworthy details:
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:
Submit The resulting Url will become 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='key'> <button>确定</button> </form>
If you are doing IE browser compatibility, please remember that the default value of button[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='key'> <input value='ok' type='submit'> </form>
You only need to return false at the end of a series of onsubmit statements to prevent it from submitting. If you want to call a method to decide whether to prevent submission, remember to return the return value of the method here:
<form onsubmit="return false;"> <input name='key'> <input value='ok' type='submit'> </form>
Related recommendations:
form form submission data problem
Questions about submitting data
Example of how to control input format in html
The above is the detailed content of Example analysis of the difference between input, submit, button and enter key to submit data. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In HTML, you can use the width and height attributes to set the size of the button element, and use the background-color attribute to set the color of the button element. The specific syntax is "button{width: width value; height: height value; background-color: color value;}".

Preparation Use vuecreateexample to create a project. The parameters are roughly as follows: use native input. Native input is mainly value and change. The data needs to be synchronized when changing. App.tsx is as follows: import{ref}from'vue';exportdefault{setup(){//username is the data constusername=ref('Zhang San');//When the input box changes, synchronize the data constonInput=;return( )=>({

How to implement the laravel input hidden field: 1. Find and open the Blade template file; 2. Use the method_field method in the Blade template to create a hidden field. The creation syntax is "{{ method_field('DELETE') }}".

Solutions for clicking the input box without a cursor: 1. Confirm the focus of the input box; 2. Clear the browser cache; 3. Update the browser; 4. Use JavaScript; 5. Check the hardware device; 6. Check the input box properties; 7. Debug JavaScript code; 8. Check other elements of the page; 9. Consider browser compatibility.

Vue is a popular JavaScript front-end framework with a responsive data binding and component system at its core. In Vue applications, the input box is one of the most commonly used UI elements. When the user enters text, we hope to listen for the carriage return event and validate the input before submitting. This article will introduce the input box enter event and verification function usage in the Vue document. 1. The carriage return event of the input box in Vue. Monitoring the carriage return event of the input box in Vue is very simple.

Vue.js is a lightweight JavaScript framework that is easy to use, efficient and flexible. It is one of the most popular front-end frameworks currently. In Vue.js, input box binding events are a very common requirement. This article will introduce the input box binding events in the Vue document in detail. 1. Basic concepts In Vue.js, the input box binding event refers to binding the value of the input box to the data object of the Vue instance, thereby achieving two-way binding of input and response. In Vue.j

Title: Using Python's input() function to get input from the user When writing a program, you often need to get input from the user. Python provides a built-in function input() that can be used to obtain and receive user input. This article will introduce how to use the input() function and some common application scenarios. The input() function is very simple to use. It accepts an optional prompt message as a parameter and waits for user input. After the user inputs, the input() function will return a

Use the <isindex> tag to add a single-line input field. The HTML<isindex> tag is used to query documents via text fields. This tag can be used anywhere, but is best placed in the head tag. NOTE: This is a deprecated tag and should not be used. Following is the list of properties − Property Value Description Prompt String Text field Label ActionURL Use example when you need to send query to different URL Try the following code − <!Doctypehtml><html
