Course contentsShow
← Web Development
Lesson 884 of 2,98216. Forms and ValidationFree lesson

Form Submission Basics

Understanding default form submission behavior, the submit event, and how browsers handle form data.

Form Submission Basics

What you'll learn: How browsers handle form submissions by default and what happens when you click that submit button.

The Default Form Behavior

When a user clicks a submit button inside a <form> element, the browser does something automatic: it collects all the input values, packages them up, and sends them to the server specified in the form's action attribute. Then it navigates to a new page with the response.

Think of it like filling out a paper form and mailing it — once you drop it in the mailbox, you wait for a reply letter. The browser refreshes the page with whatever the server sends back.

The Submit Event

Before the form actually sends, the browser fires a submit event on the form element. This is your chance to intercept the submission:

const form = document.querySelector('form');

form.addEventListener('submit', function(event) {
  console.log('Form is about to submit!');
  // The form will still submit unless we stop it
});

To prevent the default navigation behavior (useful for single-page applications or custom validation), use event.preventDefault():

form.addEventListener('submit', function(event) {
  event.preventDefault(); // Stop the browser's default submission
  console.log('Form submission prevented');
  // Now you control what happens next
});

How Browsers Package Form Data

The browser automatically collects data from input fields with a name attribute. It formats this data based on the form's method (GET or POST) and enctype attributes, then sends it to the URL in the action attribute.

Key Takeaway: By default, forms reload the page when submitted, but the submit event lets you intercept and customize this behavior with preventDefault().