Which data be post to the server when submitting a form

1,HTML Specification

In HTML specification, a section of form submission dedicated to explain which data should be post to the sever when submitting a form. User agent(such as Chrome, IE) which compliant with the specification will send form data set to the server by http protocol.

In general, form data set is a key/value pairs in which contains a number of name/value extracted by html element. Not all of html element value will be extracted, only successful controls will be extracted and combine together as form data set.

Any elements that have the attribute of name and value is a successful control, except:

Please refer to specification for details.

2,Example

If you have a web page like this:

<html>
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <input type="text" name="user" value=" " /><br /><br />
        <input type="checkbox" name="chk" value="CheckBoxA" /><span>CheckBoxA</span>
        <input type="checkbox" name="chk" value="CheckBoxB" /><span>CheckBoxB</span><br /><br />
        <input id="Radio1" type="radio" /><span>RadioA</span>
        <input id="Radio2" type="radio" /><span>RadioB</span><br /><br />
        <select id="Select1" name="Select1">
            <option>England</option>
            <option>US</option>
            <option>China</option>
        </select>
        <br /><br />
        <input id="Hidden1" name="Hidden1" type="hidden" value="HiddenValueA" />
        <input type="text" name="disableText" value=" DisableTextA" disabled="disabled" />
        <input type="text" name="invisbleText" value="invisbleText" style ="display:none;" />
    </div>
    <br /><br />
    <input id="Submit1" type="submit" value="submit" />
    </form>
</body>
</html>

We type some words and click some checkboxes, as following example. Which element’s value will be send after clicking submit button?

The answer(using firebug) to the example is:

Some question you may ask?

Q: Why RadioA do not extract as name/value pair?

A: Radio controls do not set the name attribute. if the radio control contains a atrribute called name, as RadioA, it will be extracted as name/value pair and send to server.

Q:How to extract the value in server-side by asp.net?

A:You want to extract value of user, for instance, you can use:

string userValue = this.Request.Form["user"];

Q:If the name of element duplicated, such as two of checkbox are named chk, how to separate one from another?

A:In server-side, they be separated by comma(,). If you extract the value of checkbox by follow csharp statement:

string chk = this.Request.Form["chk"];

You’ll get value as : CheckBoxA,CheckBoxB

@ 2011-06-10 08:00

Comments:

Sharing your thoughts: