Your IP address is: 64.227.125.117

Defining how to send the data via form

The <form> element defines how the data will be sent. All of its attributes are designed to let you configure the request to be sent when a user hits a submit button. The two most important attributes are action and method.

The action attribute

The action attribute defines where the data gets sent. Its value must be a valid relative or absolute URL. If this attribute isn’t provided, the data will be sent to the URL of the page containing the form — the current page.

In this example, the data is sent to an absolute URL — https://example.com:

<form action=”https://example.com”>

The method attribute

The method attribute defines how data is sent. The HTTP protocol provides several ways to perform a request; HTML form data can be transmitted via a number of different methods, the most common being the GET method and the POST method

The GET method

The GET method is the method used by the browser to ask the server to send back a given resource: “Hey server, I want to get this resource.” In this case, the browser sends an empty body. Because the body is empty, if a form is sent using this method the data sent to the server is appended to the URL.

Consider the following form:

<form action=”http://www.foo.com” method=”GET”>
<div>
<label for=”say”>What greeting do you want to say?</label>
<input name=”say” id=”say” value=”Hi”>
</div>
<div>
<label for=”to”>Who do you want to say it to?</label>
<input name=”to” id=”to” value=”Mom”>
</div>
<div>
<button>Send my greetings</button>
</div>

</form>

The POST method

The POST method is a little different. It’s the method the browser uses to talk to the server when asking for a response that takes into account the data provided in the body of the HTTP request: “Hey server, take a look at this data and send me back an appropriate result.” If a form is sent using this method, the data is appended to the body of the HTTP request.

Let’s look at an example — this is the same form we looked at in the GET section above, but with the method attribute set to POST.

<form action=”https://www.foo.com” method=”POST”>
<div>
<label for=”say”>What greeting do you want to say?</label>
<input name=”say” id=”say” value=”Hi”>
</div>
<div>
<label for=”to”>Who do you want to say it to?</label>
<input name=”to” id=”to” value=”Mom”>
</div>
<div>
<button>Send my greetings</button>
</div>
</form>