Submitting Form Fields with Duplicate Name Attributes
Question:
When submitting a form containing multiple text input fields with the same name attribute, can all field values still be retrieved from the $_POST array in PHP?
Answer:
No, only the value of the last input element with the same name will be stored in the $_POST array.
Reason:
PHP populates the $_POST array by exploding the raw query string into individual name-value pairs. When it encounters multiple name-value pairs with the same name, it overwrites the previous value with the new one.
Alternatives:
To handle multiple inputs with the same name:
Parsing the Raw Query String:
If using the raw query string, you can parse it manually using a script similar to:
$post = array(); foreach (explode('&', file_get_contents('php://input')) as $keyValuePair) { list($key, $value) = explode('=', $keyValuePair); $post[$key][] = $value; }
The above is the detailed content of Can PHP\'s `$_POST` Array Handle Multiple Form Fields with the Same Name?. For more information, please follow other related articles on the PHP Chinese website!