tags:

views:

49

answers:

3

In this form code what is the role of name="" attribute?

name="s" and name="submit".

Is it necessary to add?

<form action="/index.php" method="get">
   <fieldset>
    <legend>Search</legend>
    <label for="s"><span>Search WaSP</span>
    <input value="" name="s" id="s"></label>
    <input type="submit" value="Go!" name="submit" >
   </fieldset>
  </form>
A: 

That is what's actually sent to the server as the name in a normal form submit.

E.g. the name of the StackOverflow answer field is post-text, so the name sent to the server for this field is post-text.

For a GET or application/x-www-form-urlencoded POST, this will be the left-side of a parameter (name=value).

Matthew Flaschen
Such as with PHP (since you're sending it to index.php), your method="get" form would send php this: $_GET['s'] and $_GET['submit']. - - - And for all the PHP individuals, that'd also be available as $_REQUEST['s'] and $_REQUEST['submit']
Dan Heberden
+5  A: 

The name ("control name") will be passed into the query string when the form is submitted. This is different from the id attribute which is used to identify an element uniquely by the UA (browser).

With name, the query will be like

/index.php?s=&submit=Go!

Without name, the query will be like

/index.php
KennyTM
so it's a must have attribute of input in any technology as asp.net, php etc. am i right?
metal-gear-solid
Yeah, that's right.
Ikke
Not really "must have". If you don't specify `name`, the corresponding control will not get added to the request. Sometimes it's a good thing for a `submit` button, sometimes not.
Amadan
@Ikke - but not must have for W3C validation
metal-gear-solid
A: 

This is how they are represented in the DOM as well.

document.forms[0].s.value

Dan Heberden
It should be document.forms[0] and the default type is text.
Matthew Flaschen
Thanks for pointing out my typo :) (updated the answer) thought it was best practice to mention type attribute, but to each their own i suppose..
Dan Heberden