4

I'm writing, for the first time in PHP, a script to process a POSTed form that contains checkboxes.

As you know, checkboxes are grouped by using the same name attribute, and when multiple values occur, $_POST["some_name"] should be storing those in an array.

I tested but instead of the expected array I got one single value -- that of the last checked item.

At this point, somewhat puzzled, I look at some doc to discover that with PHP the checkbox name should end with []. Is that so? What crazy relation is there expected to exist between an HTML form and a script that processes it? Suppose I don't have access to the HTML field names? Suppose, as it is my case, I have every good reason not to alter my naming scheme just for some obscure nonsense.

Now, it seems there exists at least one solution to the issue: PHP: Retrieving value of checkboxes when name doesn't have array

I'd like to know if there's a really simple way to do without those crazy brackets, and, incidentally, your opinion on this point? Am I the only one shocked?

EDIT : OK, I can certainly work my way around this issue, but I hoped to be raising some (minor yet) fundamental point regarding some sort of "non-separation of concerns" between HTML and PHP. Obviously, that's none of PHP's business how HTML code should be written.

2
  • 1
    I just ran into the same issue and had the same exact thought! Why should the html form names be dependant on php? And what, if like you, I have no control over the form and the person creating it did it the 'normal' way (without []). I have to say I don't get why I should have to add [] to my HTML because the HTML and PHP should be independent of each other. Did you find a normal solution - one that is not to create your form in a PHP specific manner? Commented Apr 29, 2011 at 1:31
  • Unfortunately, no. I ended up adding those brackets. And I'm definitely not in love with PHP! Commented May 2, 2011 at 9:46

3 Answers 3

0

In your html code for the checkboxes:

name="mycheckboxname[]"

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

8 Comments

Precisely my point. I want to avoid foolish deeds, whenever I can.
No abuse meant, of course. I hope my addendum to the question does clarify a bit.
I didn't interpret your comment that way, not to worry. But: no, your addendum didn't clarify your question at all. What are you actually looking for here? GET and POST parameters / variables can be supplied as arrays. You do so when submitting HTML forms by using the square brackets at the end of the input field name.
What I don't get is: whenever several fields have the same name, why doesn't PHP automatically store their values in an array? And why should I "tell" PHP in such in bizarre way, that makes absolutely no sense syntactically. Why should PHP need to interpret the symbol I've chosen as a value for the name attribute, checking for the final [] ? This is silly. Suppose simultaneously some other part of PHP required the same name to end with ?@#! for some other good reason. How would you do?
PHP never even sees that. That is all handled by the browser and sent either as: an array, or as a single value. You don't need to tell PHP anything at all. You do need to tell the browser to send the fields as an array.
|
0

You should try with

<input type="checkbox" name="mycheckbox[]" value="Male" />
<input type="checkbox" name="mycheckbox[]" value="Female" />

In php side,

$myCheckBoxCollection = $_REQUEST['mycheckbox'];

Comments

0

What is your good reason for not incorporating [] in your naming scheme ? I don't think this is particularly shocking. As far as PHP is concerned, maybe you actually were expecting to only get the value from the last element with that particular name.

There are some alternatives such as the one you included in your question, or fetching the values with some javascript trickery, but I don't think you will find any that's easier than simply adding brackets to the name of your checkboxes.

Comments

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.