views:

286

answers:

6

I found that SO put the search input inside a form tag, and i found some other websites do the same, such as google, they put the search input and search button inside this:

<form action="/search" method="get" name="gs" id="tsf">

I want to just know the benefits of doing so, because it seems common and I miss it.

A: 

How else would you transmit the search term to the server? The reason the input in inside a form tag is so that the browser can POST the value to the server for processing.

Edit: In order to understand better I would suggest that you read HTML Forms and Input.

Andrew Hare
This would be most common of course, but it is *possible* to do it other ways.. parameter to search Url for example..
markt
The form's action attribute defines the name of the file to send the content to. The file defined in the action attribute usually does something with the received input.
Syed Tayyab Ali
The action attribute specifies the URL to sent the data to, not the file.
David Dorward
+3  A: 

The action attribute of the <form> element tells the browser which URL on the server is responsible for handling the form.

RichieHindle
A: 

The search input is just a generic text box, and the only way to get the information (what you're searching for) from the page to the server is to send it via a form.

www.w3schools.com and others have some great tutorials on building forms.

Theoretically you can use JavaScript to submit the information without explicitly typing out a form, but if the client has JavaScript disabled, it will never work - whereas using the HTML will work all the time

AnonJr
+7  A: 

How about you read a tutorial on HTML forms?

The data entered in the form must be send to a server. The action attribute tells the browser which server.

Also, there are two common ways to send the data: through POST (in the HTTP header) and through GET (as part of the query string, the part after the question mark in a URL). Which method must be used is specified in the method attribute.

POST is commonly used when data should be submitted only once or should be private (e.g. registering at or logging in to a site). GET is used for data that may be send as often as necessary (because the resulting URL contains a query string that one can e.g. bookmark). Example: Google search queries are sent using GET, but to log in to your GMail account POST is used. A more elaborate explanation can be found here.

Edit: below you ask why the whole page can't just be wrapped in one form tag. As divo correctly answers: you may have multiple forms that can be submitted to different servers. For example, you can provide two text fields on your webpage: one that allows one to search the site using Google, the other using Yahoo. With a little creativity other uses will come to mind.

Edit 2:

Can you point to a different tutorial? W3Schools has errors. – David Dorward 10 mins ago

If that is so (I didn't check), these two look promising:

Stephan202
Can you point to a different tutorial? W3Schools has errors.
David Dorward
A: 

This may be a bit of a stretch, but based on your website www.igurr.com, I think what you're really asking is:

Why don't I have to put a form tag on my page to get it to post back?

This is because ASP.NET automatically adds a form tag for you right after the body tag:

<form name="aspnetForm" method="post" action="/default.aspx" id="aspnetForm">
John Rasch
A: 

I understood the reason, which is very simple:

instead of putting a big general form tag for all the page, these websites just put form around the parts which will go back to the server, but why not put everything inside a form, that what i am trying to understand now.

Amr ElGarhy
Simply because you might want to have several multiple forms with different actions on a single page.
0xA3
P.S.: And don't forget to mark an answer ;-)
0xA3
Because the whole page isn't a form. Same reason you don't put the whole page inside a table or a p tag.
Rich Bradshaw