How a Form Works: form tag , action attribute & method attribute

 HTML FORMS

We can think of the internet as a network of computers which send and receive information. Computers need an HTTP request to know how to communicate. The HTTP request instructs the receiving computer how to handle the incoming information. More information can be found in our article about HTTP requests.

The <form> element is a great tool for collecting information, but then we need to send that information somewhere else for processing. We need to supply the <form> element with both the location of where the <form>‘s information goes and what HTTP request to make. Take a look at the sample <form> below:

<form action="/example.html" method="POST">
</form>

In the above example, we’ve created the skeleton for a <form> that will send information to example.html as a POST request:

  • The action attribute determines where the information is sent.
  • The method attribute is assigned a HTTP verb that is included in the HTTP request.

Note: HTTP verbs like POST do not need to be capitalized for the request to work, but it’s done so out of convention. In the example above we could have written method="post" and it would still work.

The <form> element can also contain child elements. For instance, it would be helpful to provide a header so that users know what this <form> is about. We could also add a paragraph to provide even more detail. Let’s see an example of this in code:

<form action="/example.html" method="POST">
  <h1>Creating a form</h1>
  <p>Looks like you want to learn how to create an HTML form. Well, the best way to learn is to play around with it.</p>
</form>

The example above doesn’t collect any user input, but we’ll do that in the next exercise. For now, let’s practice making the foundation of an HTML <form>!




Are there any other types of data that can be accepted by the method attribute of the form element apart from “POST”? If yes, then what functions do the others perform?


Hi @pulkitsingla :slightly_smiling_face:
I’ve found these Mozilla docs very useful:

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form

You can find your answer under the Attributes section. Hope it helps solve your question!

Comments

Popular posts from this blog

Password Input: type password attribute and relationship between name and id attribute of input tag

Adding a Label: label tag, for attribute and id attribute for input tag.