Php $ _ POST tutorial on getting html form data

Source: Internet
Author: User

The $ _ POST variable is used to collect the values in the form from method = "post. The information sent from a form with the POST method is invisible to anyone (not displayed in the address bar of the browser) and there is no limit on the amount of information sent.

Let's take a look at the simplest example.

The Code is as follows: Copy code

<Form action = "post. php" method = "post">
Name: <input type = "text" name = "username"/>
<Input type = "submit" value = "OK"/>
</Form>

If I enter hello,

The post. php file code is as follows:

The Code is as follows: Copy code

You are <? Php echo $ _ POST ["username"]?>.

The output result is

You are Hello

How can I obtain the image single-choice button for post? The example is as follows:

The Code is as follows: Copy code

<Form action = "radiopost. php" method = "post">
<Input type = "radio" name = "abcde" value = "Apple"> Apple </input> <br/>
<Input type = "radio" name = "abcde" value = "Orange"> Orange </input> <br/>
<Input type = "radio" name = "abcde" value = "Mango"> Mango </input> <br/>
<Input type = "submit" value = "OK">
</Form>

Radiopost. php file

The Code is as follows: Copy code

<? Php echo $ _ POST ["abcde"]?>

In this way, if you select one, all the values will be exported, and the checkbox will be different. You can pay attention to this.

The Code is as follows: Copy code

<Form action = "checkboxpost. php" method = "post">
<Input type = "checkbox" name = "abcde []" value = "Apple"> Apple </input> <br/>
<Input type = "checkbox" name = "abcde []" value = "Orange"> Orange </input> <br/>
<Input type = "checkbox" name = "abcde []" value = "Mango"> Mango </input> <br/>
<Input type = "submit" value = "OK">
</Form>

Checkboxpost. php file

The Code is as follows: Copy code

Print_r ($ _ POST ['abcde']);

The output is an array.

Note:
Variables sent through http post are not displayed in the URL.
The variable has no length limit.

$ _ POST and php: // input can get the value. $ HTTP_RAW_POST_DATA is empty.
$ _ POST organizes submitted data in an associated array and performs encoding, such as urldecode or even encoding conversion.
Php: // input: Raw POST data that has not been processed can be obtained by reading files from the input stream.

Php: // input allows reading original POST data. Compared with $ HTTP_RAW_POST_DATA, it puts less pressure on the memory and does not require any special php. ini settings. Php: // input cannot be used for enctype = "multipart/form-data ".
Here we will expand the post simulation method.

The Code is as follows: Copy code

<? PHP
$ Flag = 0;
// Data to be post
$ Argv = array (
'Var1' => 'abc ',
'Var2' => '');
// Construct the string to post
Foreach ($ argv as $ key => $ value ){
If ($ flag! = 0 ){
$ Params. = "&";
$ Flag = 1;
}
$ Params. = $ key. "="; $ params. = urlencode ($ value );
$ Flag = 1;
}
$ Length = strlen ($ params );
// Create a socket connection
$ Fp = fsockopen ("127.0.0.1", 80, $ errno, $ errstr, 10) or exit ($ errstr. "--->". $ errno );
// Construct the post Request Header
$ Header = "POST/mobile/try. php HTTP/1.1 ";
$ Header. = "Host: 127.0.0.1 ";
$ Header. = "Referer:/mobile/sendpost. php ";
$ Header. = "Content-Type: application/x-www-form-urlencoded ";
$ Header. = "Content-Length:". $ length ."";
$ Header. = "Connection: Close ";
// Add the post string
$ Header. = $ params ."";
// Send post data
Fputs ($ fp, $ header );
$ Inheader = 1;
While (! Feof ($ fp )){
$ Line = fgets ($ fp, 1024); // only the returned data on the page is displayed when the request packet header is removed.
If ($ inheader & ($ line = "n" | $ line = "")){
$ Inheader = 0;
}
If ($ inheader = 0 ){
Echo $ line;
}
}
Fclose ($ fp );
?>


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.