views:

13

answers:

1

Greetings,

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?

Thanks!

+1  A: 
Byron Whitlock
Thanks. Would a urlencode be sufficient to replicate whatever happens on the client code, then?
AlexeyMK
You can just use firebug or fiddler. create a simpel <form><input name=bleh></form> and then put in some text. Look on the net panel in firebug to see what the browser sends. If you aren using firefox, grab fiddler and do the same thing. I promise though, you have some javascript rewriting your strings.
Byron Whitlock
I'm sure I do, and I'll look at that. I'm more interested, though, in what function on the PHP server side replicates whatever browsers are doing. Also, am I approaching this problem the wrong way? I can't be the first to need to check which of a series of form checkboxes have been clicked.
AlexeyMK
Looking at escape - it doesn't convert spaces to underscores, but to %20's. This is what's confusing me :)
AlexeyMK
Nothing will convert spaces to underscores except javascript code on the client.
Byron Whitlock