PHP: // input to process form data with the same name value _ php-PHP Tutorial

Source: Internet
Author: User
This article mainly introduces how to use PHP: input to process form data with the same name value in php. This article is another method to process form data with the same name value, another method is provided in this article. For more information, see my blog in May last October. I analyzed how php processes the data submitted by a form with the same front-end name, at that time, the solution was to change the name value to the array type. after receiving the result, php merged the array. The disadvantage of this method is that it is not fully compatible with front-end forms. users must make changes to the forms, and the system requirement is that all upgrades are completely transparent to users. Therefore, this method is not perfect.

After nearly a year of accumulation, we found that php provides the original method for accessing the input/output stream. POST data can be obtained through php: // input:

The code is as follows:


Php: // input is a read-only stream that can access the requested raw data. In the case of POST requests, it is best to use php: // input instead of $ HTTP_RAW_POST_DATA, because it does not rely on specific php. ini commands. In this case, $ HTTP_RAW_POST_DATA is not filled by default, which requires less memory than activating always_populate_raw_post_data. When enctype = "multipart/form-data", php: // input is invalid.
Php: // the data stream opened by input can only be read once. The data stream does not support the seek operation. However, depending on the implementation of SAPI, when the request body data is saved, it can open another php: // input data stream and read it again. Generally, this is only for POST requests, rather than other request methods, such as PUT or PROPFIND.

Therefore, you can use php to obtain the form data with the same name value. 1. use php: // input to obtain the original POST data. 2. process and merge the data. 3, re-assign the processed value to the system variable $ _ POST. The following is a defined function:

Function GET_SUBMIT () {if (empty ($ _ POST) return $ _ POST; // Determine the submission type if ($ _ SERVER ["HTTP_CONTENT_TYPE"]! = 'Application/x-www-form-urlencoded ') {return $ _ POST;} // obtain the original POST value $ data = file_get_contents ("php: // input "); if (empty ($ data) return $ _ POST; // start processing $ POST = array (); $ list = explode ('&', $ data ); foreach ($ list as $ key => $ value) {// Obtain the KEY and Value of POST $ postname = urldecode (substr ($ value, 0, stripos ($ value, "="); $ postvalue = urldecode (substr ($ value, (stripos ($ value, "=") + 1 ))); // process KEY and Value values // remove spaces and [] $ postname = trim ($ postname, ', [,]'); $ postvalue = trim ($ postvalue); if (array_key_exists ($ postname, $ POST) {$ POST [$ postname] = $ POST [$ postname]. ",". $ postvalue;} else {$ POST [$ postname] = $ postvalue ;}} return $ POST ;}

Next, you only need to use "$ _ POST = GET_SUBMIT ()" as needed, and then you can use $ _ POST to obtain the data of each form after processing.

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.