Recently, there was a problem in an inquiry message management system. There were many input boxes with the same name value in the form at the front desk of the message. These boxes were filled in by users with different values. Now they need to be migrated to the PHP platform. , and it is required that no form in the front desk can be changed (because there are too many websites that use this form, so the compatibility of the transfer must be considered, even the submission address of the form cannot be changed, it must be submitted to an asp page superior). The form submission address problem can be solved directly using pseudo-static or other methods. Since the previous system was made by ASP, when ASP processes forms with the same name value, it directly connects the values submitted by the front desk with commas, but PHP is different. When it receives input with the same name, it The last one overwrites the previous value. So, how can we accept the values of all inputs with the same name without rewriting the frontend? Two ideas came to mind at that time. The first one was to pass the name of the input to the backend in the form of an array and then process it, but it was quickly rejected because this would also require changing the frontend code to allow
is changed to . The second thought is that in the PHP configuration, is there any similar setting that allows PHP to process forms with the same name value like ASP? I checked the information for a long time and couldn't find it.
In the end, I found that I could only settle for the next best thing, slightly change the front desk, and replace the name with an array. Fortunately, there are not many websites that use this method. Then the next step is to process the data in the background. Part of the name in the front desk has been changed. At this time, a situation will arise. PHP does not know whether the form submitted is a string or an array. So how to do it? I The solution 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 the value from GET or POST. If it is a string, then process the string and return it. If it is an array, then traverse the input and connect each element of the array with spaces. Then return the entire concatenated string.
In this way, the entire requirement is realized. The disadvantage is that the data must be changed for some websites that use the same name form. If you have a better method, you are welcome to leave a message to communicate with me.