SmartCodingTips

HTML Form Submission

Form submission is the process where data entered by the user in a form is sent to a server for processing. The <form> element uses attributes like action and method to control this process.

Form Action and Method

The action attribute defines the URL where the form data is sent. The method attribute specifies the HTTP method: GET or POST.


<form action="submit.php" method="post">
  <label for="name">Name:</label>
  <input type="text" id="name" name="name" required>
  <br><br>

  <label for="email">Email:</label>
  <input type="email" id="email" name="email" required>
  <br><br>

  <button type="submit">Send</button>
</form>
            

Use POST for secure data submission and GET for simple queries like search forms.

GET vs POST

  • GET: Appends data to the URL, visible in the browser. Suitable for non-sensitive data.
  • POST: Sends data in the request body. Preferred for sensitive or large data submissions.
  • Use method="post" for forms like login, registration, or feedback.

Button Types

The <button> element can have different types like submit, reset, and button.

  • submit: Submits the form data.
  • reset: Resets all fields to their initial values.
  • button: A generic button (often used with JavaScript).