This article mainly introduces how to receive multiple data instances with the same name but different values in PHP. For more information, see
This article mainly introduces how to receive multiple data instances with the same name but different values in PHP. For more information, see
I recently encountered a problem in an inquiry message management system. There are many input boxes with the same name value in the form of the message front-end. These boxes are different values filled by users, now we need to migrate to the php platform and require that we cannot change any form on the front-end (because there are too many websites using this form, we must consider transfer compatibility, even the form submission address cannot be changed. It must be submitted to an asp page ). You can use pseudo-static or other methods to directly solve the form submission address problem. Since asp was used in the past, asp directly uses commas to connect the submitted values in the foreground when processing forms with the same name value, but php does not, when it receives input with the same name, it overwrites the previous value. So, how can we achieve the same input values of all names without rewriting the foreground? At that time, two ideas emerged in my mind. The first one was to upload the input name to the background in an array and then process it, but it was quickly denied, in this case, you must modify the front-end code
Change. The second idea is whether there are similar settings in the php configuration that allow php to process the form with the same name value like asp. After checking the information for half a day, it cannot be found.
Finally, I found that I had to go back to the next step. I changed the front-end slightly and changed the name to an array. Fortunately, there were not many websites to use. The next step is the problem of data processing in the background. The front-end name is changed in part. At this time, a problem occurs. php does not know whether the string or array submitted by form is returned, so how to do it? My method is to write a function:
Function input_treat ($ input) {if (gettype ($ input) = "string") {return htmlentities (trim ($ input), ENT_QUOTES );} else if (gettype ($ input) = "array") {$ nd = ""; foreach ($ input as $ v) {$ nd. = htmlentities (trim ($ v), ENT_QUOTES ). "" ;}return $ nd;} else {return false ;}}
Use the input_treat () function to process GET or POST values. If it is a string, process the string and return it. If it is an array, traverse the input, concatenate each element of the array with spaces, and then return the entire connected string.
In this way, the entire requirement is met. The disadvantage is that some websites using the same name form must change the data. If you have a better way, please leave a message with me.