Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to deal with form data with the same name value in PHP

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article focuses on "how to deal with form data with the same name value in PHP". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to deal with form data with the same name value in PHP.

Php provides the original method to access the input / output stream, and the data of POST can be obtained through php://input.

Php://input is a read-only stream that can access the raw data of the request. 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 instructions. Also, in this case, $HTTP_RAW_POST_DATA is not populated by default, potentially requiring less memory than activating always_populate_raw_post_data. Php://input is invalid when enctype= "multipart/form-data".

Data streams opened by php://input can only be read once; data streams do not support seek operations. However, depending on the implementation of SAPI, when the request volume data is saved, it can open another php://input data stream and read it again. Typically, this situation is only for POST requests, not for other request methods, such as PUT or PROPFIND.

So the idea of using php to get form data with the same name value can be as follows: 1, getting the original POST data through php://input; 2, processing and merging the data; 3, re-assigning the processed value to the system variable $_ POST

Here is a function defined:

Function GET_SUBMIT () {if (empty ($_ POST)) return $_ POST; / / determine the submission type if ($_ SERVER ["HTTP_CONTENT_TYPE"]! = 'application/x-www-form-urlencoded') {return $_ POST;} / / get the POST raw value $data= file_get_contents ("php://input"); if (empty ($data) return $_ POST; / / start processing $POST=array (); $list=explode (' &', $data) Foreach ($list as $key= > $value) {/ / get the KEY and value values of POST $postname=urldecode (substr ($value,0, stripos ($value, "=")); $postvalue=urldecode (substr ($value, (stripos ($value, "=") + 1)); / / A pair of key and value values are processed / / blank and [] $postname=trim ($postname,', [,]'); $postvalue=trim ($postvalue); if ($postname,$POST) {$POST [$postname] = $POST [$postname]. } else {$POST [$postname] = $postvalue;}} return $POST;}

Next, as long as you use "$_ POST=GET_SUBMIT ()" where you need it, you can then use $_ POST to get the data for each form after processing.

At this point, I believe you have a deeper understanding of "how to deal with form data with the same name value in PHP". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report