0

I am currently stuck at posting checkboxes that haven't been checked and require your help. The following is the code I am currently using.

PHP

while($select_info2 = mysqli_fetch_assoc($select_info1)){

$n='1';
echo "<tr>";
    echo "<td>".$select_info2['Device_DNS_name']."</td>";
    echo "<td>".$select_info2['Uplink_port']."</td>";
    echo "<td>".$select_info2['VLAN']."</td>";
    echo "<td>".$select_info2['Teamed_network']."</td>";
    echo "<td>".$select_info2['Teamed_description']."</td>";
    for($a = $select_detailed_dns2['starting_port']; $a <= $select_detailed_dns2['ending_port']; $a++ ){
        if('10' > $a){
            if ($select_info2["port-0".$a] == "1" || $select_info2["port-0".$a] == "01" ){
                $check ='checked="checked"';
            }else{
                $check = '';
            }
            ?>
                <td><input type="checkbox" name="check_<?php echo $select_info2['ID']."[]"?>" id="check_<?php echo $select_info2['ID']?>"  <?php echo $check ?> value = "On" /></td> 
            <?php
        }else{
            if ($select_info2["port-".$a] == "1" || $select_info2["port-".$a] == "1" ){
                $check ='checked="checked"';
            }else{
                $check = '';
            }
            ?>
                <td><input type="checkbox" name="check_<?php echo $select_info2['ID']."[]"?>" id="check_<?php echo $select_info2['ID']?>"  <?php echo $check ?> value = "On" /></td>
            <?php
        }
    }
    ?>
    <td><input type="button" name="Check_All" value="Check All" onClick="CheckAll(document.checktable.check_<?php echo $select_info2['ID']?>)"> </td>
    <td><input type="button" name="Un_Check_All" value="Uncheck All" onClick="UnCheckAll(document.checktable.check_<?php echo $select_info2['ID']?>)"> </td>
    <td><input type="submit" name="update_row" value="Update Row" > </td>
    <?php
echo "</tr>";   

}

The above code generates checkboxes depending how many columns there are in the table in the database. You may be asking why the name is an array format. This is so that I can POST all values. You may also be asking why the id is the same and not different. This is so that I can target an entire row to check or uncheck. The below is how I am currently outputting all checked checkboxes:

<?php
if(isset($_POST['update_row'])){
    $c1 = $_POST['check_1'];
    var_dump($c1);
}

?>

Generated HTML

<td><input type="checkbox" name="check_1[]" id="check_1"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_1[]" id="check_1"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_1[]" id="check_1"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_1[]" id="check_1"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_1[]" id="check_1"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_1[]" id="check_1"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_1[]" id="check_1"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_1[]" id="check_1"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_1[]" id="check_1"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_1[]" id="check_1"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_1[]" id="check_1"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_1[]" id="check_1"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_1[]" id="check_1"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_1[]" id="check_1"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_1[]" id="check_1"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_1[]" id="check_1"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_1[]" id="check_1"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_1[]" id="check_1"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_1[]" id="check_1"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_1[]" id="check_1"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_1[]" id="check_1"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_1[]" id="check_1"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_1[]" id="check_1"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_1[]" id="check_1"   value = "1" /></td>
                                                                    <td><input type="button" name="Check_All" value="Check All" onClick="CheckAll(document.checktable.check_1)"> </td>
                            <td><input type="button" name="Un_Check_All" value="Uncheck All" onClick="UnCheckAll(document.checktable.check_1)"> </td>
                            <td><input type="submit" name="update_row" value="Update Row" > </td>
                            </tr><tr><td>HEPSAN03</td><td>No</td><td>NET44</td><td>No</td><td></td>                                         <td><input type="checkbox" name="check_2[]" id="check_2"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_2[]" id="check_2"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_2[]" id="check_2"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_2[]" id="check_2"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_2[]" id="check_2"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_2[]" id="check_2"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_2[]" id="check_2"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_2[]" id="check_2"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_2[]" id="check_2"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_2[]" id="check_2"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_2[]" id="check_2"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_2[]" id="check_2"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_2[]" id="check_2"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_2[]" id="check_2"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_2[]" id="check_2"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_2[]" id="check_2"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_2[]" id="check_2"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_2[]" id="check_2"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_2[]" id="check_2"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_2[]" id="check_2"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_2[]" id="check_2"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_2[]" id="check_2"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_2[]" id="check_2"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_2[]" id="check_2"   value = "1" /></td>
                                                                    <td><input type="button" name="Check_All" value="Check All" onClick="CheckAll(document.checktable.check_2)"> </td>
                            <td><input type="button" name="Un_Check_All" value="Uncheck All" onClick="UnCheckAll(document.checktable.check_2)"> </td>
                            <td><input type="submit" name="update_row" value="Update Row" > </td>
                            </tr><tr><td>HEPSAN03</td><td>No</td><td>PRIV44</td><td>No</td><td></td>                                            <td><input type="checkbox" name="check_4[]" id="check_4"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_4[]" id="check_4"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_4[]" id="check_4"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_4[]" id="check_4"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_4[]" id="check_4"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_4[]" id="check_4"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_4[]" id="check_4"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_4[]" id="check_4"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_4[]" id="check_4"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_4[]" id="check_4"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_4[]" id="check_4"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_4[]" id="check_4"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_4[]" id="check_4"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_4[]" id="check_4"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_4[]" id="check_4"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_4[]" id="check_4"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_4[]" id="check_4"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_4[]" id="check_4"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_4[]" id="check_4"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_4[]" id="check_4"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_4[]" id="check_4"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_4[]" id="check_4"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_4[]" id="check_4"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_4[]" id="check_4"   value = "1" /></td>
                                                                    <td><input type="button" name="Check_All" value="Check All" onClick="CheckAll(document.checktable.check_4)"> </td>
                            <td><input type="button" name="Un_Check_All" value="Uncheck All" onClick="UnCheckAll(document.checktable.check_4)"> </td>
                            <td><input type="submit" name="update_row" value="Update Row" > </td>
                            </tr><tr><td>HEPSAN03</td><td>No</td><td>NET40</td><td>No</td><td></td>                                         <td><input type="checkbox" name="check_5[]" id="check_5"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_5[]" id="check_5"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_5[]" id="check_5"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_5[]" id="check_5"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_5[]" id="check_5"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_5[]" id="check_5"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_5[]" id="check_5"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_5[]" id="check_5"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_5[]" id="check_5"   value = "1"  /></td> 
                                                                                <td><input type="checkbox" name="check_5[]" id="check_5"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_5[]" id="check_5"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_5[]" id="check_5"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_5[]" id="check_5"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_5[]" id="check_5"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_5[]" id="check_5"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_5[]" id="check_5"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_5[]" id="check_5"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_5[]" id="check_5"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_5[]" id="check_5"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_5[]" id="check_5"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_5[]" id="check_5"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_5[]" id="check_5"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_5[]" id="check_5"   value = "1" /></td>
                                                                                <td><input type="checkbox" name="check_5[]" id="check_5"   value = "1" /></td>
                                                                    <td><input type="button" name="Check_All" value="Check All" onClick="CheckAll(document.checktable.check_5)"> </td>
                            <td><input type="button" name="Un_Check_All" value="Uncheck All" onClick="UnCheckAll(document.checktable.check_5)"> </td>
                            <td><input type="submit" name="update_row" value="Update Row" > </td>
                            </tr>

Hope you can Help Thank You

Deepak
  • 25
  • 10
  • With JQuery, you can select all checkboxes, whenever or not they have been checked. `$('input:checkbox')` https://api.jquery.com/checkbox-selector/ – TCHdvlp Mar 10 '16 at 10:43
  • @TCHdvlp Can you be more precise on how to solve my problem? – Deepak Mar 10 '16 at 10:44
  • Sorry I can't because I don't understand your problem. You want to post checkboxes that haven't been selected ? Only the ones not selected, all checkboxes ? You want to get boxes id, value, name ??? – TCHdvlp Mar 10 '16 at 10:47
  • What is the value of `$select_info2['ID']` ? – Pierre C. Mar 10 '16 at 10:50
  • @TCHdvlp So basically I want to post all checkboxes whether checked or not. However, the ones that have not been checked should have a value of "Off". The problem however is that the name is an array format and the ID is only different once a new row begins – Deepak Mar 10 '16 at 10:51
  • @PierreC. Its the ID number from database. – Deepak Mar 10 '16 at 10:51
  • Ok, let me create a little jsfiddle to show you how to manage checked property – TCHdvlp Mar 10 '16 at 10:53
  • @TCHdvlp Do keep in mind while creating the jsfiddle that the id has to be the same and the name has to be in an array format so that it replicates the problem – Deepak Mar 10 '16 at 10:59
  • Hope it helps https://jsfiddle.net/TCHdevlp/abvdvmfq/2/ – TCHdvlp Mar 10 '16 at 11:08
  • @TCHdvlp Unfortunately it doesn't work for me. – Deepak Mar 10 '16 at 11:12
  • Can you provide a sample of the generated html ? (sorry I'm not familiar with php). And what are you especting server side (format) – TCHdvlp Mar 10 '16 at 11:13
  • 1
    "So basically I want to post all checkboxes whether checked or not. However, the ones that have not been checked should have a value of "Off"." — That isn't how checkboxes work. Give them unique **values** so you can tell which ones were checked when they get submitted. – Quentin Mar 10 '16 at 11:21
  • It's the weirdest thing that I've ever seen with checkboxes. They exists to submit the checked values , not all values. To send all values you need to check them previously. It's the only way (or change to other form element like inputs) – Marcos Pérez Gude Mar 10 '16 at 11:24
  • Your html is broken. Anyway, There is still a way to identify checkboxes in a unique way, using `data attribute` http://html5doctor.com/html5-custom-data-attributes/. – TCHdvlp Mar 10 '16 at 11:28
  • In the duplicated link the solution is as I said before, try to add inputs. – Marcos Pérez Gude Mar 10 '16 at 11:31

1 Answers1

0

Can you not set default="0" or "2" or something and check for it?

Maybe this could help to.. Post the checkboxes that are unchecked

Community
  • 1
  • 1
  • This is a comment, not an answer – Marcos Pérez Gude Mar 10 '16 at 11:23
  • If it works, it is an answer, i have had this issue before and am sure I used this method. Of course there is many ways to do such a thing, it's a simple enough question requiring a simple enough answer. I have also included a link to a far more explanative answer as I feel it answers the question without myself going on about the solution Marcos – Ron Appleton Mar 10 '16 at 11:27
  • 1
    I upvote to compensate the downvote that you grow (I don't downvote you). But link-only answers are not a quality answer in stackoverflow. Try to read the help http://stackoverflow.com/help . If you think is a duplicate, flag it as duplicate, not answer with a link. Good luck – Marcos Pérez Gude Mar 10 '16 at 11:30
  • Agree this is not a quality answer, but the link provided is a good solution to the problem, and I was going to post something similar myself. @RonAppleton Expand your answer with some specific help to the questioner's problem, and you might even get a few upvotes! – beercohol Mar 10 '16 at 11:33
  • "Good solution" are not the correct words. It's a weird hack to a weird request. – Marcos Pérez Gude Mar 10 '16 at 11:37
  • Thanks, will take the comments on-board and in future address specific details, thanks very much both. – Ron Appleton Mar 10 '16 at 17:35