Use PHP's super variable $ _ POST to obtain HTML Form data

Source: Internet
Author: User

When the method of HTML Form is get, $ _ GET is used to obtain the data of HTML Form.

When the method of HTML Form is post, $ _ POST is used to obtain the data of HTML Form.

For the difference between get and post of HTML Form, see the difference between get and post of HTML Form.

Obtain the data in the HTML Form text input box (input type = "text ").
The following is an HTML file. This HTML contains an HTML Form, which is mainly used for users to input user names.Copy codeThe Code is as follows: <Body>
<Form action = "post. php" method = "post">
Name: <input type = "text" name = "username"/>
<Input type = "submit" value = "OK"/>
</Form>
</Body>
</Html>

When You enter a name in the text box of the HTML Form, such as "Jacky", and then click OK, the page will jump to post. php. The output result is You are Jacky .. The source code of post. php is as follows:Copy codeThe Code is as follows: <Body>
You are <? Php echo $ _ POST ["username"]?>.
</Body>
</Html>

Obtain the data of the Form Control by taking the name value of the Form Control.

For example, "username" is the name value of the text input box of the Form Control,

<Input type = "text" name = "username"/>
You can use $ _ POST ["username"] to obtain the data in the text input box.

<? Php echo $ _ POST ["username"]?>
Obtain HTML Form (HTML Form) single dataset (input type = "radio") data
You can obtain the value of the single-sheet signature by taking the name value of the single-sheet signature.

The following is an HTML file containing a single sheet. The Code is as follows:Copy codeThe Code is as follows: <Body>
<Form action = "radiopost. php" method = "post">
<Input type = "radio" name = "fruit" value = "Apple"> Apple </input> <br/>
<Input type = "radio" name = "fruit" value = "Orange"> Orange </input> <br/>
<Input type = "radio" name = "fruit" value = "Mango"> Mango </input> <br/>
<Input type = "submit" value = "OK">
</Form>
</Body>
</Html>

In this HTML file, select either item, for example, "Orange", and click "OK". the browser will jump to radiopost. php, and the output result of radiopost. php is Orange. The source code of radiopost. php is as follows:Copy codeThe Code is as follows: <Body>
<? Php echo $ _ POST ["fruit"]?>
</Body>
</Html>

In $ _ POST ["fruit"], fruit is the name value of the form ticket.

Obtain the data of the HTML Form check box (input type = "checkbox ")
You can select multiple values through the HTML Form check box. Therefore, $ _ POST produces more than one value, which is an array.

When writing the name value of the HTML Form check box, note that [] must be added at the end of the name value.

For example, name = "fruit []":Copy codeThe Code is as follows: <Body>
<Form action = "checkboxpost. php" method = "post">
<Input type = "checkbox" name = "fruit []" value = "Apple"> Apple </input> <br/>
<Input type = "checkbox" name = "fruit []" value = "Orange"> Orange </input> <br/>
<Input type = "checkbox" name = "fruit []" value = "Mango"> Mango </input> <br/>
<Input type = "submit" value = "OK">
</Form>
</Body>
</Html>

The source code of checkboxpost. php is as follows:Copy codeThe Code is as follows: <Body>
<? Php
Echo count ($ _ POST ["fruit"]), "<br/> ";
Foreach ($ _ POST ["fruit"] as $ value)
{Echo $ value, "<br/> ";
}
?>
</Body>
</Html>

If you select Orange and Mango and click OK, the browser will jump to checkboxpost. php: Use the count function to obtain the number of elements in the array $ _ POST ["fruit"]. If you select two items, the result is 2, then, use foreach to output the $ _ POST ["fruit"] value of each element, that is, the target value of the user option, Orange and Mango.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.