1

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?

2 Answers 2

3

The accepted answer by Byron Whitlock is wrong. PHP is indeed the culprit here. See the PHP manual:

Dots and spaces in variable names are converted to underscores. For example <input name="a.b" /> becomes $_REQUEST["a_b"].

Also refer to the answers of this similar question.

Sign up to request clarification or add additional context in comments.

Comments

-1

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.

6 Comments

Thanks. Would a urlencode be sufficient to replicate whatever happens on the client code, then?
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.
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.
Looking at escape - it doesn't convert spaces to underscores, but to %20's. This is what's confusing me :)
it would be nice if this incorrect but accepted answer could be fixed to prevent spreading misinformation.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.