简体   繁体   English

从MySQL选择Bootstrap下拉菜单

[英]Bootstrap Dropdown Select From MySQL

Newbie question, what's wrong with my code, i can't populate dropdown select from mysql table. 新手问题,我的代码有什么问题,我无法从mysql表中填充下拉列表。 please advise, thank you 请指教,谢谢

<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>

在此处输入图片说明

if i separate my php code on a new php page, the query working fine 如果我在新的php页面上分离我的php代码,则查询工作正常 在此处输入图片说明

it's working fine with html manual code (not from database) 使用html手动代码(不是来自数据库)可以正常工作

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

在此处输入图片说明

Put '' on your $rows[sku_code] 在您的$rows[sku_code] “” $rows[sku_code]

like 喜欢

$rows['sku_code']

and concatenate your code. 并连接您的代码。

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

Change your code line 更改您的代码行

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

to

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

Now It should work fine. 现在它应该工作正常。

I found the problem My file is .HTML I put php inside the HTML 我发现了问题,我的文件是.HTML,我将php放入了HTML

so after googling, i found the solusion to edit httpd.conf by adding: 所以在谷歌搜索之后,我发现通过添加以下内容来编辑httpd.conf:

AddType application/x-httpd-php .html .htm  

reference: PHP script not working in HTML file 参考: PHP脚本在HTML文件中不起作用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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