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

SeguerSeguer of the VoidSydney, AustraliaRegistered User regular
edited October 2007 in Help / Advice Forum
Who was the genius that thought that up?

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)

Seguer on

Posts

  • DrFrylockDrFrylock Registered User regular
    edited October 2007
    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:

    <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.

    DrFrylock on
  • SeguerSeguer of the Void Sydney, AustraliaRegistered User regular
    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!

    Thanks.

    Seguer on
  • DrFrylockDrFrylock Registered User regular
    edited October 2007
    Seguer wrote: »
    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
  • SeguerSeguer of the Void Sydney, AustraliaRegistered User regular
    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 :D so thanks anyway!

    Seguer on
  • SporkAndrewSporkAndrew Registered User, ClubPA regular
    edited October 2007
    If they're dynamically generated you can just pass a list of the checkboxes in a hidden variable:
    <input type="hidden" name="checkboxes" value="checkbox_name_1|checkbox_name_2|[...]"/>
    

    Then on the other side explode that value and check:

    [php]
    $checkboxes = explode("|", $_POST);
    foreach ($checkbox as $check_name) {
    if ($_POST[$checkbox]){
    [...]
    }
    }
    [/php]

    SporkAndrew on
    The one about the fucking space hairdresser and the cowboy. He's got a tinfoil pal and a pedal bin
  • SeguerSeguer of the Void Sydney, AustraliaRegistered User regular
    edited October 2007
    Could, but oh well. Already did it the other way. And that php code block looks horrible oO (the colours)

    Seguer on
  • SporkAndrewSporkAndrew Registered User, ClubPA regular
    edited October 2007
    Oh dear God I just noticed that. I just wanted to press the fancy "php" button.

    SporkAndrew on
    The one about the fucking space hairdresser and the cowboy. He's got a tinfoil pal and a pedal bin
  • robotbeboprobotbebop Registered User regular
    edited October 2007
    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!
  • SeguerSeguer of the Void Sydney, AustraliaRegistered User regular
    edited October 2007
    Doh. I think I saw that array thing while I was researching the checkboxes, but I paid no attention to it.

    Seguer on
Sign In or Register to comment.