简体   繁体   中英

unable to post values of multiple check boxes created form while loop

My question maybe has been asked many times before but i couldn't apply the given solutions on my project yet

I'm trying to post only the selected children check-boxes not the parents

my page code is:

     <?php  $db = mysqli_connect('localhost', 'root', '123', 'test'); ?>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
     <html xmlns="http://www.w3.org/1999/xhtml">

     <head>
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
     <script src="jquery.min.js"></script>
     <script src="cbFamily.js"></script>
     </head>

    <body>
    <form name="myform" method="post" action="test.php">
     <section class="demo2" style="margin-top:2px;">

     <?php
     $query=mysqli_query($db,"SELECT * FROM tblmainjobs");

     while ($row = mysqli_fetch_assoc($query)):
     ?>
    <section>
    <h3><label><input type="checkbox" /> <?php echo $row["mainjob"]; ?> 
    </label></h3>

    <?php 
    $query2=mysqli_query($db,"SELECT userjob FROM users WHERE 
    '".$row["mainjob"]."' = users.mainjob GROUP BY users.userjob");
    while ($row2 = mysqli_fetch_assoc($query2)):
    ?>
    <div class="children">
    <label><input type="checkbox" name="checkbox" value="<?php echo 
    $row2["userjob"]; ?>"/> <?php echo $row2["userjob"]; ?></label>       

    <?php endwhile; ?>

    </section>

    <?php endwhile; ?>

    <script type="text/javascript">  <!-- this function for selecting all 
    children checkboxes once the parents checkbox bieng selected -->
    $("h3 input:checkbox").cbFamily(function (){
    return $(this).parents("h3").next().find("input:checkbox");
    });
    </script>

    </section>  

    <br>
    <div><input class="submit" type="submit" name="submit" value="submit"/> 
    </div>
    </form>

    </body>
    </html>

my page looks like this test.php

my entire project is attached here https://www.sendspace.com/file/6ikcaa

You could use javascript to disable the "parent" checkboxes before they get posted to the server.

Add a class to your parent checkboxes to make for an easy jquery selector:

<input type="checkbox" class="parent" />

Then some javascript to disable the checkboxes:

$(".parent").prop("disabled",true);

Use array:

<input type="checkbox" name="checkbox[]" value="<?php echo 
$row2["userjob"]; ?>"/> <?php echo $row2["userjob"]; ?>

notice the [] in the name attribute.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM