Just getting into PHP web development. I’ve got an HTML form where a user checks some series of dynamically-generated checkboxes, and submits via POST. On the PHP side, I want to check which of the check-boxes were clicked.
I have an array $full_list, and am doing something like
$selected_checkboxes = array_filter($full_list, function($item) {
array_key_exists($item, $_POST);
}
I run into problems when a list item is named, for example “Peanut Butter”, since in the POST array it is named “Peanut_Butter”.
I could certainly just str_replace ” ” with “_” before checking array_key_exists, but I imagine that there is a more fundamental encoding problem here; specifically, I’m not sure of exactly what layer transforms normal strings in HTML Forms (value=”Peanut Butter”) into “Peanut_Butter”.
So:
- what layer is responsible for this conversion? Is it the browser?
- what are the exact conversion rules, and is there a PHP function out there that will replicate that exact conversion?
PHP doesn’t do this. There is something on the client side that is converting spaces to underscores.
The browser should encode each variable using the equivalent of urlencode(). PHP will automatically decode these strings so it is transparent for the programmer.
edit
The equivalent in javaScript is escape(). But it is very very likely there is some js code manually converting spaces to underscores.