Skip to main content

How to Make an HTML Form with a Submit Button

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

A <form> with a text box and a <button type="submit"> is all it takes to collect something from the people who visit a page. When they click the button, the browser sends each field's name and value, by default by adding them to the page's address.

Syntax​

Syntax
<form action="<address>">
<input name="<field name>">
<button type="submit"><button text></button>
</form>
PartWhat it does
<form>Holds the fields that are sent together.
action="<address>"Where the form is sent. Leave it out and the form is sent to the page it's on.
<input name="<field name>">A text box. Its name is the name its value is sent under.
<button type="submit">The button that sends the form.
<button text>The text on the button.

MDN's form page lists all the form's attributes.

The Example​

The example page asks for a name, with a text box and a Send button. When you send it, the name appears in the page's address.

Step 1: Write the Code​

Create a file named index.html in a text editor, such as Visual Studio Code:

index.html, an HTML page with a form holding a label Name for the text box with the id name, a text box with the id and name name, and a Send button of type submit.

index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Form</title>
</head>
<body>
<form>
<label for="name">Name</label>
<input id="name" name="name">
<button type="submit">Send</button>
</form>
</body>
</html>

The label is the text box's caption: its for attribute names the text box's id. The text box's name="name" is what the browser sends with the value.

Step 2: Type a Name​

Double-click index.html to open it in your browser, then type Ana into the text box:

The page in Chromium: the label Name, a text box with Ana typed in it, and a Send button.

The screenshot comes from Chromium 159, the open-source browser that Chrome is built on.

Step 3: Send the Form​

Click Send. The page loads again with an empty text box, and the address in the address bar now ends with index.html?name=Ana.

What Happened​

  • The browser sent the form to the same page, because the form has no action.
  • It added ?name=Ana to the address: the text box's name, an equals sign, and what you typed. A form sends its data this way unless it says otherwise.
  • The text box is empty again, because the browser loaded the page anew, at the new address.

Common Mistakes​

MistakeWhat happensDo this instead
Leaving out name on a fieldThe value isn't sent: with no name on the text box, the address ended with just a ?Give every field you want sent a name
Expecting id to be sentOnly name is sent: the text box above still had its id, and nothing was sentUse id for the label, and name for the data

Troubleshooting​

SymptomLikely causeFix
The address ends with only ?The fields have no nameAdd name to each field
The typed text is gone after sendingThat's expected: the browser loaded the page againRead the values from the address, or send the form to another page

FAQ​

Does pressing Enter send the form?​

Yes. Typing Ben in the text box and pressing Enter sent the form just like the button did: the address ended with ?name=Ben.

How do I send the form to another page?​

Set action. With <form action="thanks.html">, clicking Send opened thanks.html?name=Ana.

What happens to the data after that?​

Nothing, unless something reads it. An HTML page can't store data on its own: a program on a web server, or a script in the page, reads the values from the address and does something with them.

Conclusion​

A form, a field with a name and a submit button are enough to send data. Without an action, the form is sent to the same page, with each name=value pair added to the address.

Additional Resources​