PHP:使用下拉列表输出数据

时间:2015-01-24 14:20:38

标签: php mysql

我很难搞清楚这一点。

这是我的带下拉列表的html,它正常工作。但问题是,我没有关于如何输出itemprofile表的Stock Column中的Stock数据的线索,每次我在下拉列表中选择不同的产品时,Stock值也应该相应地改变。

<html>
<body>
<form>
<select name="product" />
<?php
$mysqli = new mysqli("localhost", "root", "","bsystem");
$results = $mysqli->query("SELECT * FROM itemprofile");
    if ($results) { 
        while($obj = $results->fetch_object())
        {
            echo '<option value="'.$obj->productname.'">'.$obj->productname.'</option>';
        }
    }
    ?>
</select>

Available Stock: <?php NO CLUE WHAT TO DO HERE?>
</form>
</body>
</html>

我的数据库的内容:

3 columns: id, productname, stock
            1   item1         50
            2   item2         30
            3   item3         10

希望你们能帮助我解决这个问题。谢谢!

编辑:我的代码到目前为止。

<html>
<head>
<script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
</head>

<body>

<select id="selectItem">
    <option>-- Select item --</option>
    <?php
    $mysqli = new mysqli("localhost", "root", "","bsystem");
    $results = $mysqli->query("SELECT * FROM itemprofile");
    $itemsStock = array(); # create an array where you will keep your stock values for each item
    if ($results) { 
        while($obj = $results->fetch_object()){
            echo '<option value="'.$obj->productname.'">'.$obj->productname.'</option>';
            $itemsStock[$obj->id] = $obj->stock; # fill array with stock values 
        }
    }
    ?>
</select>

Available stock: <div id="stockValue"></div>

<script>

var stockValues = <?php echo json_encode($itemsStock);?>; // transfer array that contains item stock values from php to the javascript array

    $("#selectItem").change(function(){ // on every DropDown change
        var ItemID = $(this).val(); // get selected item id
        console.log(ItemID);
        var ItemStockValue = stockValues[ItemID]; // search stock value in your array of stock values by using ItemID
        $("#stockValue").html(ItemStockValue); // update Available stock for selected item
    });
</script>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

您可以使用jQuery来完成。您在代码中有注释,以便您可以理解此解决方案的逻辑。通过此,您还可以在下拉列表更改时更新多个值。代码可能有一些语法错误,但这可以解决您在下拉列表更改时输出数据的问题。

<select id="selectItem">
    <option>-- Select item --</option>
    <?php
    $mysqli = new mysqli("localhost", "root", "","bsystem");
    $results = $mysqli->query("SELECT * FROM itemprofile");
    $itemsStock = array(); # create an array where you will keep your stock values for each item
    if ($results) { 
        while($obj = $results->fetch_object()){
            echo '<option value="'.$obj->id.'">'.$obj->productname.'</option>'; // THIS LINE CAUSED THE PROBLEM, i echoed productname in value attribute, instead of id
            $itemsStock[$obj->id] = $obj->stock; # fill array with stock values 
        }
    }
    ?>
</select>

Available stock: <div id="stockValue"></div>

<script>

var stockValues = <?php echo json_encode($itemsStock);?>; // transfer array that contains item stock values from php to the javascript array

    $("#selectItem").change(function(){ // on every DropDown change
        var ItemID = $(this).val(); // get selected item id
        var ItemStockValue = stockValues[ItemID]; // search stock value in your array of stock values by using ItemID
        $("#stockValue").html(ItemStockValue); // update Available stock for selected item
    });
</script>

请记住包含jQuery。希望它能帮助你:))