从sql select选项中填充另一个select

时间:2015-07-08 22:39:50

标签: php jquery sql select options

我在选择初始下拉列表后填写第二个下拉列表时遇到问题。使用静态变量时,当我选择左侧选项时,它会填充正常,但它没用,并且在右侧总是MBNA。

[7-Eleven v] [MBNA v]

如果我尝试使用基于第一个下拉列表的变量,则它是空白的。

[7-Eleven v] []

<?php
        // config
        include 'main.php';     
        ?>

        <select name="category" id="vend_info"> 
        <?php 
        while ($myrow = $resVendor->fetch_assoc()) {
        $GLOBALS['vendor'] = $myrow['name'];    
        ?> 
        <option rel="accessories"><?php echo $GLOBALS['vendor'];?></option>  

        <?php 
        } 
        ?>
        </select> 



        <?php
        $table = "VendorTable";           
        $sql = "SELECT defaultPayment FROM " . $table . " WHERE name=? ";
        $stmt = $mysqli->prepare($sql);
        if (!$stmt) {
            echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
            exit();
        }

        // Bind parameters for markers, where (s = string, i = integer, d = double,  b = blob)
        $vendor = "3M";  //static 
        //$vendor = $GLOBALS['vendor'];  //dynamic
        $stmt->bind_param('s', $vendor);

        // Execute the statement
        if (!$stmt->execute()) {
            echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
            exit();
        }

        // Get the results
        if (!$resDefPay = $stmt->get_result()) {
            echo "Getting result set failed: (" . $stmt->errno . ") " . $stmt->error;
            exit();
        }           

        // Close the statement
        $stmt->close();
            ?>


        <select name="items" class="cascade" > 
        <?php 
        while ($myrow = $resDefPay->fetch_assoc()) {
        ?> 
        <option class="accessories"><?php echo $myrow['defaultPayment'];?></option>   

        <?php 
        } 
        ?> 
        </select>       

我正在使用jquery来显示第二个选项。

 $(document).ready(function(){
var $cat = $('select[name=category]'),
    $items = $('select[name=items]');

$cat.change(function(){
    var $this = $(this).find(':selected'),
        rel = $this.attr('rel'),
        $set = $items.find('option.' + rel);

    if ($set.size() < 0) {
        $items.hide();
        return;
    }

    $items.show().find('option').hide();

    $set.show().first().prop('selected', true);
});
});  

我很感激有关如何使用sql select options的任何想法。

0 个答案:

没有答案
相关问题