根据id PHP绑定下拉列表

时间:2016-01-23 00:46:44

标签: php

我想根据下面的数据库显示下拉列表。 (bom table)

例如,当我选择'表'从下拉列表中,它将显示bom_description,bom_quantity和UOM。

bom_id |bom_description |product_id |finish_product |bom_quantity |UOM
1        Table Tops        1            Table             1        /PC
2        Table Legs        2            Chair             4        /PC
3        Chair Seat        3                              1        /PC
4        Chair Back        4                              1        /PC
5        Chair Legs        5                              4        /PC

选择产品:表格(下拉列表)

(HTML表格会显示这些)

bom_description |bom_quantity |UOM
Table Tops        1             /PC
Table Legs        4             /PC

当我点击Chair时,它会显示

选择产品:主席(下拉列表)

bom_description |bom_quantity |UOM
Chair Seat        1             /PC
Chair Back        1             /PC
Chair Legs        4             /PC

现在,这是我的代码,但显示内容的html表没有出现。

    <script src="script/jquery-1.11.1.min.js"></script>
    <script>
        $(document).ready(function () {
            $(".itemTypes").change(function () {
                if (this.value == 0) {
                    $("tr").show();
                }
                else {
                    $("tr").hide();
                    $(".header").show();
                    $("." + this.value).show();
                }
            });
        });
    </script>

<?php
    session_start();

if (!isset($_SESSION['username'])) {
    header('location: homepage.php');
}

//connect to database
include ("dbFunctions.php");

$queryBOM = "SELECT * FROM bom";

$resultBOM = mysqli_query($link, $queryBOM) or die(mysqli_error($link));
mysqli_close($link);
?>

Select Product:
                        <select class="itemTypes">
                            <option value="0">
                                all types
                            </option>
                            <?php
                            while ($row1 = mysqli_fetch_array($resultBOM)) {
                                ?>
                                <option value="<?php echo $row1['finish_product']; ?>">
                                    <?php echo $row1['finish_product']; ?>
                                </option>
                            <?php } ?></select>
                        <p>


                            <table border="1">
                                <tr>
                                    <th>BOM Description</th>
                                    <th>Quantity</th>
                                    <th>UOM</th>
                                    <th colspan="2">Action</th>

                                    <?php
                                    while ($row = mysqli_fetch_assoc($resultBOM)) {
                                        $bom_description = $row['bom_description'];
                                        $bom_quantity = $row['bom_quantity'];
                                        $UOM = $row['UOM'];
                                        ?>

                                        <tr>
                                            <td><center><?php echo $bom_description; ?></center></td>
                                            <td><center><?php echo $bom_quantity; ?></center></td>
                                            <td><center><?php echo $UOM; ?></center></td>
                                            <td><center>
                                                    <form method="post" action="editBOM.php">
                                                        <input type="hidden" name="bomID" value="<?php echo $bom_id; ?>" />
                                                        <input type="image" src="images/editicon.png" name="edit" value="edit" style="width:30px;height:30px;"/>
                                                </center></form>
                                            </td>
                                            <td><center>
                                                    <form method="post" action="dodeleteBOM.php">
                                                        <input type="hidden" name="bomID" value="<?php echo $bom_id; ?>" />
                                                        <input type="image" src="images/deleteicon.png" name="delete" value="delete" style="width:25px;height:25px;"/>
                                                </center></form>
                                                </center></td>
                                            <?php
                                        }
                                        ?>
                                    </tr>

如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

You can do it in following way:-

<script src="//code.jquery.com/jquery-1.12.0.min.js"></script>
    <script>
        $(document).ready(function () {
            $(".itemTypes").change(function () {
                var vall= $(this).val();
                if ( vall== 0) {
                    $("tr").show();
                }
                else {
                    $('form#abc').submit();
                }
            });
        });
    </script>

<?php
    session_start();

if (!isset($_SESSION['username'])) {
    header('location: homepage.php');
}

//connect to database
include ("dbFunctions.php");
$post_data='';
if(isset($_POST['value'])){
    $post_data = $_POST['value'];
    $queryBOM1 = "SELECT * FROM bom WHERE bom_description LIKE '%".$_POST['value']."%'";
}else{
$queryBOM1 = "SELECT * FROM bom";   
}
$queryBOM2= "SELECT * FROM bom";

$resultBOM = mysqli_query($link, $queryBOM2) or die(mysqli_error($link));
$resultBOM1 = mysqli_query($link, $queryBOM1) or die(mysqli_error($link));
mysqli_close($link);
?>

Select Product:
                       <form action = "<?php echo $_SERVER['REQUEST_URI'];?>" method="POST" id="abc">
                        <select class="itemTypes" name="value">
                            <option value="0">
                                all types
                            </option>
                            <?php
                            while ($row1 = mysqli_fetch_array($resultBOM)) {
                                ?>
                                <option value="<?php echo $row1['finish_product']; ?>" <?php if($row1['finish_product']==$post_data){echo "selected";}?>>
                                    <?php echo $row1['finish_product']; ?>
                                </option>
                            <?php }?></select>
</form>
                        <p>
                            <table border="1">
                                <tr>
                                    <th>BOM Description</th>
                                    <th>Quantity</th>
                                    <th>UOM</th>
                                    <th colspan="2">Action</th>

                                    <?php
                                    while ($row = mysqli_fetch_assoc($resultBOM1)) {
                                        $bom_description = $row['bom_description'];
                                        $bom_quantity = $row['bom_quantity'];
                                        $UOM = $row['UOM'];
                                        ?>

                                        <tr>
                                            <td><center><?php echo $bom_description; ?></center></td>
                                            <td><center><?php echo $bom_quantity; ?></center></td>
                                            <td><center><?php echo $UOM; ?></center></td>
                                            <td><center>
                                                    <form method="post" action="editBOM.php">
                                                        <input type="hidden" name="bomID" value="<?php echo $bom_id; ?>" />
                                                        <input type="image" src="images/editicon.png" name="edit" value="edit" style="width:30px;height:30px;"/>
                                                </center></form>
                                            </td>
                                            <td><center>
                                                    <form method="post" action="dodeleteBOM.php">
                                                        <input type="hidden" name="bomID" value="<?php echo $bom_id; ?>" />
                                                        <input type="image" src="images/deleteicon.png" name="delete" value="delete" style="width:25px;height:25px;"/>
                                                </center></form>
                                                </center></td>
                                            <?php
                                        }
                                        ?>
                                    </tr>