Checkbox array not POSTing

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
k_d
Forum Newbie
Posts: 4
Joined: Thu Oct 23, 2008 8:19 am

Checkbox array not POSTing

Post by k_d »

Using php 5.1.6, Apache/2.2.3 (Fedora), and a simple test form containing an array of checkboxes, I'm unable to get the values to post. This code works on another machine using a newer version of php (5.2.something), and it doesn't seem to be a php.ini configuration problem (I've tested the working version's php.ini with the, uh, not-working version's, and I have the same problem).

Code follows:

Code: Select all

 
<?php
  print_r($_POST);
?>
 
<FORM ACTION="post_test.php" METHOD=POST>
<TABLE border="1" width="70%" >
<TR>
<td><input type="checkbox" name="checkies[]" value="This is a value"></td>
<td><input type="checkbox" name="checkies[]" value="This is also a value"></td>
</TR>
</TABLE>
<input type="submit" value="Submit">
</form>
 
When running the above (and checking both boxes), I get the following output from print_r():

Code: Select all

 
Array
(
    [checkies] => 
)
 
This upsets me. :(

Any ideas? Your help is greatly appreciated!
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Checkbox array not POSTing

Post by aceconcepts »

Try

Code: Select all

 
print_r($_POST['checkies']);
 
k_d
Forum Newbie
Posts: 4
Joined: Thu Oct 23, 2008 8:19 am

Re: Checkbox array not POSTing

Post by k_d »

There's no output when I try that, either.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Checkbox array not POSTing

Post by aceconcepts »

Try changing your submit button to:

Code: Select all

<input type="submit" value="Submit" name="submit">
And then at the top of your script write:

Code: Select all

 
if(isset($_POST['submit']))
{
   echo'<p>Success</p>';
   print_r($_POST['checkies']);
}
else
{
   echo'<p>button not clicked</p>';
}
 
k_d
Forum Newbie
Posts: 4
Joined: Thu Oct 23, 2008 8:19 am

Re: Checkbox array not POSTing

Post by k_d »

It printed success, but the array was empty.

Any other ideas? Thanks!
User avatar
Syntac
Forum Contributor
Posts: 327
Joined: Sun Sep 14, 2008 7:59 pm

Re: Checkbox array not POSTing

Post by Syntac »

Did you select any of the checkboxes?

I noticed something weird about this a while ago. If the checkbox isn't selected when you submit the form, it doesn't show up in $_POST.

[EDIT] Probably best to use isset() when testing for checkbox values.
k_d
Forum Newbie
Posts: 4
Joined: Thu Oct 23, 2008 8:19 am

Re: Checkbox array not POSTing

Post by k_d »

Yes, I checked the checkboxes.
User avatar
Syntac
Forum Contributor
Posts: 327
Joined: Sun Sep 14, 2008 7:59 pm

Re: Checkbox array not POSTing

Post by Syntac »

I've never done anything elaborate with forms, so pardon my ignorance: What's the effect of having "[]" in the input.name attribute?
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Checkbox array not POSTing

Post by aceconcepts »

"[]" passes the form element as an array. So you can have multiple fields named identically and their values are all sent as an array - so you're not just passing the very last value in the form.
Jimbo23
Forum Newbie
Posts: 2
Joined: Fri Oct 31, 2008 1:17 am

Re: Checkbox array not POSTing

Post by Jimbo23 »

I'm running into the same thing (?!)

This was working fine before I put the code in a function, I've globalled the arrays to no avail.

Am working on more testing, but just wanted you to know that others have seen the same weird behaviour that doesn't match what is supposed to work. Will advise on any solutions I find.
Jimbo23
Forum Newbie
Posts: 2
Joined: Fri Oct 31, 2008 1:17 am

Re: Checkbox array not POSTing

Post by Jimbo23 »

Okay, got it fixed. The foreach works exactly as expected with
foreach($_POST['check'] as $key => $value)
{ whatever code you want
};

The (dumb user error) that I did was missing globalling a variable, plus the submit button needs to be after all inputs in the form. I wanted the button higher on the page for usability, and I could use Javascript to allow more than one button, but from what I've found, W3C compliance (and empirical evidence) show that the submit button just won't pass along values -- in this case an array of checkboxes -- if any of the checkboxes are after the submit button in your code, but it may not be obvious except for missing data.

Again, I've seen some Javascript examples to solve this button issue, but I want a php/server-side backup in case someone turns off Javascript, which just means a bit of reformatting, and it's all done. A small compromise for a bit savings in messing with this.

Hope that this helps.
Post Reply