The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.
PHP: Only Checked Checkboxes pass values
Seguerof the VoidSydney, AustraliaRegistered Userregular
I have a script that dynamically generates checkboxes, called Active, to determine if an address is active or not. If it's checked, then the address is active. But, if you try to edit this address to make it inactive by unchecking the checkbox, it doesn't send the name of the checkbox and its value!
Is there a way around this besides having two checkboxes that toggle each other? (an Active and Inactive checkbox)
It's been a while since I did this, but I'm pretty sure that browsers don't submit data for an unchecked checkbox. One thing you can do is to put a hidden field in the form with a name that's similar to the one with the checkbox and then you know if there was a checkbox there, but if you don't get a matching value, then you know that it was unchecked. So:
If you see a value for mycheckbox_hidden but not mycheckbox, then you know there was a checkbox called mycheckbox but it wasn't checked.
DrFrylock on
0
Seguerof the VoidSydney, AustraliaRegistered Userregular
edited October 2007
Hmm.. I'd actually done that before I tried doing it this way, heh. But then I took it out as I figured "hey, that's just extraneous code!" but I guess it's not. Damn!
Hmm.. I'd actually done that before I tried doing it this way, heh. But then I took it out as I figured "hey, that's just extraneous code!" but I guess it's not. Damn!
Thanks.
Well your other option is just to see if there's a value defined or not for a given name. Now if it's not, one of two things happened: the box wasn't checked, or there was no such checkbox in the form. There's no way to tell the difference, of course, and this assumes that you know the names of all the checkboxes in the form a priori, but if you know there was a checkbox there called "available" and there's no value for "available" then it wasn't checked.
DrFrylock on
0
Seguerof the VoidSydney, AustraliaRegistered Userregular
edited October 2007
Yeah, unfortunately because they are dynamically generated, I'd have to somehow pass on a list of those in my POST stuff. I've pretty much got the other method working already though so thanks anyway!
Also, in PHP, if you name checkboxes like <input type="checkbox" name="myboxes[]"> $_GET/$_POST/$_REQUEST["myboxes"] will be an array.
So if (!isset($_POST["myboxes"])) returns true then you know there aren't any checkboxes, and you don't have to deal with any nasty string parsing code
EDIT: Helpful hints, using $_REQUEST may be more convenient, but it is less secure. You can name ANY form input field like "foo[]" and get it back as an array
robotbebop on
Do not feel trapped by the need to achieve anything, this way you achieve everything.
Oh, hey I'm making a game! Check it out: Dr. Weirdo!
0
Seguerof the VoidSydney, AustraliaRegistered Userregular
edited October 2007
Doh. I think I saw that array thing while I was researching the checkboxes, but I paid no attention to it.
Posts
<input type="hidden" name="mycheckbox_hidden" value="doesntmatter"/>
<input type="checkbox" name="mycheckbox" value="something">
If you see a value for mycheckbox_hidden but not mycheckbox, then you know there was a checkbox called mycheckbox but it wasn't checked.
Thanks.
Well your other option is just to see if there's a value defined or not for a given name. Now if it's not, one of two things happened: the box wasn't checked, or there was no such checkbox in the form. There's no way to tell the difference, of course, and this assumes that you know the names of all the checkboxes in the form a priori, but if you know there was a checkbox there called "available" and there's no value for "available" then it wasn't checked.
Then on the other side explode that value and check:
[php]
$checkboxes = explode("|", $_POST);
foreach ($checkbox as $check_name) {
if ($_POST[$checkbox]){
[...]
}
}
[/php]
So if (!isset($_POST["myboxes"])) returns true then you know there aren't any checkboxes, and you don't have to deal with any nasty string parsing code
EDIT: Helpful hints, using $_REQUEST may be more convenient, but it is less secure. You can name ANY form input field like "foo[]" and get it back as an array
Oh, hey I'm making a game! Check it out: Dr. Weirdo!