SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Evangelist pip's Avatar
    Join Date
    Jun 2001
    Location
    Cape Town, South Africa
    Posts
    548
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Empty Checkboxes does not return a key in $_POST

    Hi Guys,

    I have a form that uses input checkboxes which, when submitted, is being retrieved by means of a while loop, traversing $_POST with each(). See below:

    PHP Code:
    reset($_POST);
    while (list (
    $key$val) = each ($_POST)) {
    echo 
    "$key => $val<br />\n";


    However, when a checkbox is UNCHECKED, no key, nor value is passed through to the $_POST superglobal. My loop is to automatically generate SQL queries, based on submitted data, and by using the key as field name. So if a checkbox was initially checked, and the user unchecks it, nothing is returned, and therefore no change made to the database.

    Please tell me that someone has a clue of how to find out if a checkbox is unchecked.


    I'm trying to avoid client-side scripting to force to value through in a text field.



    Tks,
    - Pip
    ---------------------------------------------------------------------------------
    Nothing takes the taste out of peanut butter quite like unrequited love.

  2. #2
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Insert a hidden field with the same name before the checkbox:

    PHP Code:
    <input type='hidden' name='x' value='0'>
    <
    input type='checkbox' name='x' value='1'
    So your script receive either "x=0" or "x=1".

  3. #3
    SitePoint Wizard mark_W's Avatar
    Join Date
    Mar 2004
    Location
    West Midlands, United Kingdom
    Posts
    2,631
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pip
    Hi Guys,

    I have a form that uses input checkboxes which, when submitted, is being retrieved by means of a while loop, traversing $_POST with each(). See below:

    PHP Code:
    reset($_POST);
    while (list (
    $key$val) = each ($_POST)) {
    echo 
    "$key => $val<br />\n";


    However, when a checkbox is UNCHECKED, no key, nor value is passed through to the $_POST superglobal. My loop is to automatically generate SQL queries, based on submitted data, and by using the key as field name. So if a checkbox was initially checked, and the user unchecks it, nothing is returned, and therefore no change made to the database.

    Please tell me that someone has a clue of how to find out if a checkbox is unchecked.


    I'm trying to avoid client-side scripting to force to value through in a text field.



    Tks,
    This is a bit off topic but I always liked your avatar, looks cool with the santa hat!

  4. #4
    SitePoint Evangelist pip's Avatar
    Join Date
    Jun 2001
    Location
    Cape Town, South Africa
    Posts
    548
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by stereofrog
    Insert a hidden field with the same name before the checkbox:

    PHP Code:
    <input type='hidden' name='x' value='0'>
    <
    input type='checkbox' name='x' value='1'
    So your script receive either "x=0" or "x=1".
    Wicked! What a neat trick. How exactly does the Checkbox value override the hidden field's value? That's quite interesting.

    Thanx dude!
    - Pip
    ---------------------------------------------------------------------------------
    Nothing takes the taste out of peanut butter quite like unrequited love.

  5. #5
    SitePoint Evangelist pip's Avatar
    Join Date
    Jun 2001
    Location
    Cape Town, South Africa
    Posts
    548
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mark_W
    This is a bit off topic but I always liked your avatar, looks cool with the santa hat!


    Thanks! I thought I'd go a little more festive
    - Pip
    ---------------------------------------------------------------------------------
    Nothing takes the taste out of peanut butter quite like unrequited love.

  6. #6
    SitePoint Wizard mark_W's Avatar
    Join Date
    Mar 2004
    Location
    West Midlands, United Kingdom
    Posts
    2,631
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pip


    Thanks! I thought I'd go a little more festive
    Well, he looks a lot less evil with that nice hat!

  7. #7
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pip
    How exactly does the Checkbox value override the hidden field's value? That's quite interesting.
    With unchecked box your uri looks like "script.php?x=0" and $_GET['x'] apparently equals 0.

    With checked box , both params are passed and uri would be "script.php?x=0&x=1", but $_GET['x'] is "1" because php reads from left to right and takes the rightmost value.

  8. #8
    SitePoint Evangelist pip's Avatar
    Join Date
    Jun 2001
    Location
    Cape Town, South Africa
    Posts
    548
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by stereofrog
    With unchecked box your uri looks like "script.php?x=0" and $_GET['x'] apparently equals 0.

    With checked box , both params are passed and uri would be "script.php?x=0&x=1", but $_GET['x'] is "1" because php reads from left to right and takes the rightmost value.
    Geeslike... how insane is that! Left to Right logic together with a little bit of thinking does the trick. I would never have thought of it!

    Thanks again!
    - Pip
    ---------------------------------------------------------------------------------
    Nothing takes the taste out of peanut butter quite like unrequited love.

  9. #9
    SitePoint Evangelist pip's Avatar
    Join Date
    Jun 2001
    Location
    Cape Town, South Africa
    Posts
    548
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mark_W
    Well, he looks a lot less evil with that nice hat!
    Yea.. I also thought Smiley Face vs Terminator might be too much of a shock for most. The hat makes Terminator look like he's changed sides, just for Christmas

    - Pip
    ---------------------------------------------------------------------------------
    Nothing takes the taste out of peanut butter quite like unrequited love.

  10. #10
    SitePoint Zealot Koobi's Avatar
    Join Date
    Nov 2003
    Location
    Sri Lanka
    Posts
    156
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Edit:

    Edited to correct mistakes

    Quote Originally Posted by pip
    Wicked! What a neat trick. How exactly does the Checkbox value override the hidden field's value? That's quite interesting.

    Thanx dude!

    Because the first checkbox value (ValueA) is defined and only then, the second checkbox value (ValueB) is defined.

    So initially, $_POST['x'] contains ValueA and when checkbox two is checked, it will hold a value which will be ValueB.


    Personally I think it's better to check for NULL.:
    PHP Code:
    <?php
        
    foreach($_POST as $key => $value)
        {
            if(
    $value !== NULL)
            {
                echo 
    $key ' = ' $value '<br />';
            }
        }
    ?>
    Also, if you like it shorter, you can just do this:
    PHP Code:
    <?php
        
    foreach($_POST as $key => $value)
        {
            echo (
    $value !== NULL$key ' = ' $value '<br />' '';
        }
    ?>
    The three euqal signs check for type (eg: integer, boolean, array, float, string, etc) as well as value (4, TRUE, array('foo', 'bar'), 1.5, 'myString') and when a checkbox is not selected, it returns to PHP a NULL (boolean in type, NULL in its value) and so we can use this.
    If you use two equal signs (only compares values), NULL will be read as a boolean false as well as the integer zero (or is zero only for false? I could be wrong here, you can test it out and find out if you want to know ).
    using var_dump($value); will help you determine type as well as value

    I hope this is what you were looking for


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •