Get checked and unchecked checkboxes value

11,149

Solution 1

Checkboxes send their value if they are checked and are not sent at all if they are not.

You should have:

<input type="checkbox" id="1" name="box[]" value="1" />
<input type="checkbox" id="2" name="box[]" value="2"  />
<input type="checkbox" id="3" name="box[]" value="3"  />

So the values will be 1, 2 and 3 instead of on, on and on. Then you can tell which ones are checked as they won't all be the same.

If you really want your data structure to be array(0=>'on', 1=>'off', 2=>'off') then you could do:

$foo = array();
for ($i = 1; $i <= 3; $i++) {
    $foo[$i] = in_array($i, $_GET['box']) ? 'on' : 'off';
}

Solution 2

A checkbox will be sent to the server (when the form is submitted) only if it's checked, otherwise it's not submitted

also change

onclick="pickIt('1');" /
Share:
11,149
PHP Ferrari
Author by

PHP Ferrari

BS(CS) - 07 FUUAST Lead Apps. Engineer

Updated on July 27, 2022

Comments

  • PHP Ferrari
    PHP Ferrari over 1 year

    This is my script:

    HTML Code:

    <script>
    function pickIt(pId){
        if(document.getElementById(pId).checked==true){
            document.getElementById(pId).checked=false;
        }else{
            document.getElementById(pId).checked = true;
        }
        submitForm();
        return true;
    }
    </script>
    <img src="images/bagua-square.gif" border="0" usemap="#Map2" />
    <map name="Map2" id="Map2">
        <area shape="rect" coords="201,14,284,100" onclick="pickIt(1);" />
        <area shape="rect" coords="202,104,284,190" onclick="pickIt(2);" />
        <area shape="rect" coords="202,195,284,283" onclick="pickIt(3);" />
    </map>
    <div style="display:none;">
        <input type="checkbox" id="1" name="box[]" />
        <input type="checkbox" id="2" name="box[]" />
        <input type="checkbox" id="3" name="box[]" />
    </div>
    

    PHP Code:

    <?php
        print_r($_POST['box']);
    ?>
    

    When I click on box id 1 pickIt() function turn checkbox 1 to on. And the php shows array(0=>'on')

    But I also want to get checkbox value which are not checked such that php will show array(0=>'on', 1=>'off', 2=>'off')

    Actually i want to get all checkboxes with their status on and off because i am using these id in mysql db to update record status on or off. please guide.

  • PHP Ferrari
    PHP Ferrari over 11 years
    well i have changed to onclick="pickIt('1');" but it still show the old result.
  • PHP Ferrari
    PHP Ferrari over 11 years
    Actually i want to get all checkboxes with their status on and off because i am using these id in mysql db to update record status on or off. please guide.
  • Quentin
    Quentin over 11 years
    "Php must know how many (and which) checkboxes there are" — only if you tell it. That information isn't in the submitted form data.
  • Quentin
    Quentin over 11 years
    "the $_POST values that are missing are unchecked boxes." — Since they all have the same value, you can only tell how many are missing, not which ones.
  • PHP Ferrari
    PHP Ferrari over 11 years
    hmm... little bit closer. Its mean that i have to write two sql queries first to update all records status to set off then to set status to on for only those rows which are in array. :(
  • Frank van Wijk
    Frank van Wijk over 11 years
    @Quentin: 1: True, but that's why Form caching is so useful :) 2: Sorry, I mean the $_POST keys. The keys all differ, do they?
  • Quentin
    Quentin over 11 years
    — Not unless they have different values, which they don't.