Bootstrap Dropdown从MySQL中选择

时间:2015-11-19 08:27:44

标签: php mysql twitter-bootstrap combobox html-select

新手问题,我的代码有什么问题,我无法从mysql表中填充dropdown select。请指教,谢谢

<html>
<head>
    <link rel="stylesheet" type="text/css" href="DataTables/Bootstrap-3.3.5/css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="DataTables/DataTables-1.10.10/css/jquery.dataTables.min.css">
</head>

<body>  
    <div id="wrapper">
        <nav class="navbar navbar-default navbar-static-top" role="navigation" style="margin-bottom: 0">
            <div class="container-fluid">
                <div class="navbar-header">

                </div>
                <div class="collapse navbar-collapse" id="bs-dtb_master_item-navbar-collapse-1">
                    <ul class="nav navbar-nav">
                        <li><a href="index.html">Home</a></li>
                        <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Data <span class="caret"></span></a>
                            <ul class="dropdown-menu">
                                <li><a href="master-item.html">Item</a></li>
                                <li><a href="master-brand.html">Brand</a></li>
                                <li><a href="master-supplier.hatml">Supplier</a></li>
                                <li><a href="master-customer.html">Customer</a></li>
                                <li><a href="master-expedition.html">Expedition</a></li>
                            </ul>
                        </li>
                        <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Transaksi <span class="caret"></span></a>
                            <ul class="dropdown-menu">
                                <li><a href="receiving.html">Receiving</a></li>
                                <li><a href="update-data.html">Update Data</a></li>
                                <li><a href="delivery-order.html">Delivery Order</a></li>
                                <li><a href="packing-slip.html">Packing Slip</a></li>
                            </ul>
                        </li>
                    </ul> 
                </div>
            </div>
        </nav>
    </div>

    <div class="container-fluid">
        <div class="row">
            <div class="col-md-12">
                <div class="row">
                    <div class="col-md-12">
                        <div class="page-header">

                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-12">

                    </div>
                </div>
                <div class="row">
                    <div class="col-md-12">
                        <!-- START TOMBOL ACTION -->
                        <!-- Button trigger modal -->
                        <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal">
                            Add New
                        </button>
                        <br><br>
                        <!-- END TOMBOL ACTION -->

                        <!-- START MODAL -->
                        <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" action="input_item.php" method="post">
                            <div class="modal-dialog modal-lg" role="document">
                                <div class="modal-content">
                                    <div class="modal-header">
                                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                        <h4 class="modal-title" id="myModalLabel">Input Item</h4>
                                    </div>
                                    <div class="modal-body">
                                        <!-- START FORM INPUT -->
                                        <form class="form-horizontal">
                                            <div class="form-group">
                                                <label class="col-sm-3 control-label">Item Code</label>
                                                <div class="col-sm-3">
                                                    <input type="text" class="form-control" placeholder="Item Code" required="required" name="item_code">
                                                </div>
                                            </div>
                                            <div class="form-group">
                                                <label class="col-sm-3 control-label">Item Name</label>
                                                <div class="col-sm-4">
                                                    <input type="text" class="form-control" placeholder="Item Name" required="required" name="item_name">
                                                </div>
                                            </div>
                                            <div class="form-group">
                                                <label class="col-sm-3 control-label">SKU</label>
                                                <div class="col-sm-3">
                                                    <select class="form-control">
                                                        <?php
                                                        #Include the connect.php file
                                                        include ('C:/xampp/htdocs/b/incl/connect.php');
                                                        $mysqli = new mysqli($hostname, $username, $password, $database);
                                                        /* check connection */
                                                        if (mysqli_connect_errno())
                                                            {
                                                            printf("Connect failed: %s\n", mysqli_connect_error());
                                                            exit();
                                                            }
                                                        $query = "SELECT * FROM master_sku";
                                                        $result = mysql_query($query);
                                                        while ($rows = mysql_fetch_array($result)) {
                                                        echo "<option value=" .$rows['sku_code']. ">" .$rows['sku_name']. "</option>";
                                                        }
                                                        ?> 
                                                    </select>
                                                </div>
                                            </div>
                                            <div class="form-group">
                                                <label class="col-sm-3 control-label">Supplier Item Code</label>
                                                <div class="col-sm-4">
                                                    <input type="text" class="form-control" placeholder="Supplier Item Code" name="vendor_item_code">
                                                </div>
                                            </div>
                                            <div class="form-group">
                                                <div class="col-sm-offset-2 col-sm-10">
                                                    <button type="submit" class="btn btn-primary">Submit</button>
                                                </div>
                                            </div>
                                        </form>
                                        <!-- END FORM INPUT -->
                                    </div>
                                </div>
                            </div>
                        </div>


                        <!-- END MODAL -->

                        <!-- START DATA TABEL -->
                        <div class="panel panel-default">
                            <div class="panel-heading">Item Management</div>
                            <div class="panel-body">
                                <table id="dtb_master_item" class="table table-striped" cellspacing="0" width="100%">
                                    <thead>
                                        <tr>
                                            <th>Item Code</th>
                                            <th>Item  Name</th>
                                            <th>SKU</th>
                                            <th>Vendor Item Code</th>
                                        </tr>
                                    </thead>
                                </table>
                            </div>
                        </div>
                        <!-- END DATA TABEL -->
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- CORE JS -->
    <script src="DataTables/jQuery-1.11.3/jquery-1.11.3.min.js"></script>
    <script type="text/javascript" src="js/jquery.min.js"></script>
    <script type="text/javascript" src="DataTables/Bootstrap-3.3.5/js/bootstrap.min.js"></script>

    <!-- DATATABLES JS -->  
    <script type="text/javascript" src="DataTables/DataTables-1.10.10/js/jquery.dataTables.min.js"></script>
    <script type="text/javascript" src="DataTables/DataTables-1.10.10/js/dataTables.bootstrap.min.js"></script>

    <script>
        $(document).ready(function() {

        // START DATATABLES MASTER ITEM 
        $('#dtb_master_item').dataTable( {
        "processing": true,
        "serverSide": true,
        "ajax": "incl/ssp_master_item.php",
         "columns": [
            { "width": "10%" },
            { "width": "50%" },
            { "width": "10%" },
            { "width": "30%" }
          ]
        } );

        // END DATATABLES MASTER ITEM   

        } );
    </script>
</body>

enter image description here

如果我在新的php页面上分离我的PHP代码,查询工作正常 enter image description here

它可以正常使用html手动代码(而非数据库)

        <select class="form-control">
                    <option>1</option>
                    <option>2</option>
                    <option>3</option>
        </select>

enter image description here

3 个答案:

答案 0 :(得分:2)

将''放在$rows[sku_code]

$rows['sku_code']

并连接你的代码。

   echo "<option value=" .$rows['sku_code']. ">" .$rows['sku_name']. "</option>";

答案 1 :(得分:0)

更改您的代码行

echo "<option value="$rows[sku_code]">$rows['sku_name']</option>";

echo "<option value=" . $rows['sku_code'] . ">" . $rows['sku_name'] . "</option>";

现在它应该可以正常工作。

答案 2 :(得分:0)

我发现了问题 我的文件是.HTML 我把PHP放在HTML里面

所以在谷歌搜索之后,我发现了通过添加:

编辑httpd.conf的问题
AddType application/x-httpd-php .html .htm  

参考:PHP script not working in HTML file