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.

Community
  • 1
  • 1
  • 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? – Karen Apr 29 '11 at 01:31
  • Unfortunately, no. I ended up adding those brackets. And I'm definitely not in love with PHP! – Nicolas Le Thierry d'Ennequin May 02 '11 at 09:46

3 Answers3

0

In your html code for the checkboxes:

name="mycheckboxname[]"

Aaria Carter-Weir
  • 1,639
  • 12
  • 17
  • 1
    Precisely my point. I want to avoid foolish deeds, whenever I can. – Nicolas Le Thierry d'Ennequin Apr 22 '11 at 09:39
  • No abuse meant, of course. I hope my addendum to the question does clarify a bit. – Nicolas Le Thierry d'Ennequin Apr 22 '11 at 10:10
  • 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. – Aaria Carter-Weir Apr 22 '11 at 13:02
  • 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? – Nicolas Le Thierry d'Ennequin Apr 22 '11 at 13:33
  • 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. – Aaria Carter-Weir Apr 22 '11 at 13:55
  • No, not at all. The POST is sent as a string made of key/value pairs, where keys need not be unique. For instance: `spoken=French&spoken=English&spoken=Spanish`. What we're discussing here is how PHP handles this string. – Nicolas Le Thierry d'Ennequin Apr 22 '11 at 14:10
  • Fair point. Might be worth checking the docs on this. I actually believe PHP is doing it right. I'm unsure if that's even a PHP thing actually, it could well be an apache behaviour. – Aaria Carter-Weir Apr 22 '11 at 15:36
  • This question is a bit fluffy guys. As far as PHP is concerned to my knowledge, post keys need to be unique OR an array. You send an array by putting `[]` in the post field name. End of. The accepted answer is not the most idea. – Aaria Carter-Weir Mar 18 '16 at 02:34
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.

Lawyerson
  • 919
  • 1
  • 7
  • 25
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'];
Sanjay Mohnani
  • 990
  • 7
  • 18