简体   繁体   中英

Select All Checkbox from MYSQL

Today I'm currently using " Checkbox " and just a little problem. I have a dropdown list in my first page, I choose the items "DropDown 1" and then I click the Submit button after that it will go to the next page. So then, the second page will load all items under the "DropDown 1" using a checkbox only.

My problem is:

How can I check them all by using a checkbox "Check All", where the items in "DropwDown 1" are came from my database [MySQL].

Here's my code in page two:

    <input type="checkbox" name="all" id="all" /> <label for='all'>All</label>
<?php
$dropdown_value = (string)$_POST["id"];
echo "<br/>";
if ($dropdown_value == 'All Building') 
{

     $all = mysql_query("SELECT fldBldgName FROM tblbuildings");
        while ($row = mysql_fetch_array($all))
           {
                echo "<tr><td>";

                echo "<input type='checkbox' name='play[]' class='chk_boxes1' value='" . $row['fldBldgName'] . "'>";
                echo $row['fldBldgName'];

                echo "</td></tr><br/>";
           }
}

    ?>
    <script>
  $(document).ready(function(){
$('input[name="all"]').bind('click', function(){
var status = $(this).is(':checked');
$('input[type="checkbox"]').attr('checked', status);
});
});
    </script>

Try listening to the change event on the .chk_boxes element. You should also use prop instead of attr when working with "binary" properties like `checked':

$('.chk_boxes').on('change', function(){
    $('.chk_boxes1').prop('checked', $(this).prop('checked'));
});

http://api.jquery.com/prop/

You also need to wrap all your tr / td elements in a table element.:

<table>
<?php
...
?>
</table>

There's no need for br elements after a tr . tr will always start on a new line.

look checked Attribute

echo "<input type='checkbox' class='chk_boxes1' name='play[]' value='".$row['fldBldgName']. "' checked>";

and why a <br> in a table?

EDIT

try this:

    echo '<div><input type="checkbox" class="checkall"> Check all</div>';
 $all = mysql_query("SELECT fldBldgName FROM tblbuildings");
        while ($row = mysql_fetch_array($all))
           {

           echo "<div><input type='checkbox' name='play[]' class='chk_boxes1' value='" . $row['fldBldgName']."'>";
           echo $row['fldBldgName'];."</div>";

           }

and jQuery function:

$(function () {
    $('.checkall').on('click', function () {
        $(this).closest('fieldset').find(':checkbox').prop('checked', this.checked);
    });
});

http://jsfiddle.net/H37cb/210/

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