Page 1 of 1
Checkbox array not POSTing
Posted: Thu Oct 23, 2008 8:28 am
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():
This upsets me.
Any ideas? Your help is greatly appreciated!
Re: Checkbox array not POSTing
Posted: Thu Oct 23, 2008 8:49 am
by aceconcepts
Re: Checkbox array not POSTing
Posted: Fri Oct 24, 2008 7:31 am
by k_d
There's no output when I try that, either.
Re: Checkbox array not POSTing
Posted: Fri Oct 24, 2008 4:22 pm
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>';
}
Re: Checkbox array not POSTing
Posted: Sat Oct 25, 2008 7:33 am
by k_d
It printed success, but the array was empty.
Any other ideas? Thanks!
Re: Checkbox array not POSTing
Posted: Sat Oct 25, 2008 9:54 am
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.
Re: Checkbox array not POSTing
Posted: Tue Oct 28, 2008 2:04 pm
by k_d
Yes, I checked the checkboxes.
Re: Checkbox array not POSTing
Posted: Tue Oct 28, 2008 2:43 pm
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?
Re: Checkbox array not POSTing
Posted: Wed Oct 29, 2008 4:44 am
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.
Re: Checkbox array not POSTing
Posted: Fri Oct 31, 2008 1:21 am
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.
Re: Checkbox array not POSTing
Posted: Sun Nov 02, 2008 4:48 pm
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.