How to Create a Submit Button in HTML: Simple Guide
To create a submit button in HTML, use the
<button type="submit"> element or an <input type="submit"> element inside a form. This button sends the form data to the server when clicked.Syntax
The submit button in HTML can be created using two main elements:
<button type="submit">Submit</button>: A button element withtype="submit"sends the form data when clicked.<input type="submit" value="Submit">: An input element of type submit that acts as a button with a label from thevalueattribute.
Both must be placed inside a <form> element to work properly.
html
<form action="/submit"> <button type="submit">Submit</button> </form>
Output
A button labeled 'Submit' that sends form data when clicked.
Example
This example shows a simple form with a submit button. When clicked, it sends the form data to the server URL specified in the action attribute.
html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Submit Button Example</title> </head> <body> <form action="/submit" method="post"> <label for="name">Name:</label> <input type="text" id="name" name="name" required> <button type="submit">Send</button> </form> </body> </html>
Output
A form with a text input labeled 'Name' and a button labeled 'Send'. Clicking the button submits the form data.
Common Pitfalls
- Placing the submit button outside the
<form>element will prevent it from submitting the form. - Using
type="button"instead oftype="submit"creates a button that does not submit the form. - Not specifying
typeon a<button>defaults totype="submit", but it's best to be explicit.
html
<form action="/submit"> <!-- Wrong: type="button" does not submit --> <button type="button">Submit</button> <!-- Correct: type="submit" submits the form --> <button type="submit">Submit</button> </form>
Output
Two buttons labeled 'Submit'; only the one with type="submit" sends the form data.
Quick Reference
| Element | Usage | Notes |
|---|---|---|
| Creates a submit button inside a form | Can contain HTML and text | |
| Creates a submit button with a label | Label set by value attribute | |
| type attribute | "submit" triggers form submission | Default for |
| Placement | Must be inside | Otherwise, button won't submit |
Key Takeaways
Use
Always place the submit button inside the
Explicitly set type="submit" on buttons for clarity and consistent behavior.
Avoid using type="button" for submit actions as it does not submit the form.
The submit button sends form data to the URL specified in the form's action attribute.