如何从Mysql数据库中填充复选框?

时间:2014-08-12 08:18:27

标签: php jquery mysql checkboxlist html.dropdownlistfor

当用户从下拉列表中选择一个选项时,我想检索数据到复选框列表。因此,主要类别将加载到下拉列表中,并且子类别假定要加载到复选框列表中。现在,我整理出来将数据填充到另一个下拉列表中。这是我的代码:

JQuery代码:

 <script type="text/javascript">
    $(document).ready(function() {

    $("#parent_cat").change(function() {
        $(this).after('<div id="loader" style="position:inline;"><img src="device manager/img/loading.gif" alt="loading subcategory" /></div>');
        $.get('user manager/loadsubcat.php?parent_cat=' + $(this).val(), function(data) {
            $("#sub_cat").html(data);
            $('#loader').slideUp(200, function() {
                $(this).remove();
            });
        }); 
    });

});
</script>

PHP代码:

  <tr>
    <?php 
    include('config.php'); 
    $query_parent = mysql_query("SELECT * FROM tblclient") or die("Query failed: ".mysql_error());
    ?>
     <form method="get">
     <tr>
        <td><label>Client</label></td>

        <td>
            <select name="parent_cat" id="parent_cat">
                <option selected="selected" disabled="disabled">Select a Client</option>
                <?php while($row = mysql_fetch_array($query_parent)): ?>
                    <option value="<?php echo $row['clientId']; ?>"><?php echo $row['clientName']; ?></option>
                <?php endwhile; ?>
            </select>
        </td>
     </tr>
        <br/><br/>
     <tr>
        <td><label>Branch</label></td>

        <td>
            <select name="sub_cat" id="sub_cat" multiple>
                <option selected="selected" disabled="disabled">Select a Branch</option>
            </select>
        </td>
     </tr>
     </form>

加载子类别列表PHP代码:

<?php 
include('config.php');

$parent_cat = $_GET['parent_cat'];

$query = mysql_query("SELECT B.*, C.* FROM tblbranch B INNER JOIN tblclientbranch CB on CB.branchId = B.branchId INNER JOIN tblclient C ON CB.clientId = C.clientId WHERE C.clientId = {$parent_cat}");
while($row = mysql_fetch_array($query)) {
    echo "<option value='$row[branchId]'>$row[branchName]</option>";
}

?>

有谁知道如何将子类别填充到复选框列表中?

任何解决方案都会很棒。 感谢您阅读我的长代码!

1 个答案:

答案 0 :(得分:1)

类似

<input type="checkbox" name="branch" value="$row[branchid]">$row[branchname]<br>