bug^君 2016-04-19 01:18 采纳率: 25%
浏览 4

下拉列表ajax

Can some one look over my code and see what is causing the second drop down to not be populating? I am not quite sure why. Thank you in advance.

View:

    <div id="finder">
    <div class="form">
    <select id="brand" name="brand" onchange="myFunction()" >
        <option value="">&nbsp;&nbsp;- Printer Brand -&nbsp;&nbsp;</option>
        <?php foreach ($categories as $category_1) { ?>
        <?php if ($category_1['category_id'] == $category_id) { ?>
        <option value="<?php echo $category_1['category_id']; ?>" selected="selected"><?php echo $category_1['name']; ?></option>
        <?php } else { ?>
        <option value="<?php echo $category_1['category_id']; ?>"><?php echo $category_1['name']; ?></option>
        <?php } ?>
        <?php } ?>
    </select>
    </div>

    <br>

<div class="form">
    <select name="printerSeries" id="printerSeries">
    <option value="">&nbsp;&nbsp;- Printer Series -&nbsp;&nbsp;</option>

    </select>
  </div>
  <br>

Javascript ajax call

<script type="text/javascript">
function myFunction(){
var brand_id = document.getElementById("brand").value;
        $.ajax({
        url: 'index.php?route=common/home/getSeries&brand_id=' + brand_id, 
        type: 'post',
        dataType: 'json',
        success: function(json) {
            $('#printerSeries').append('<option value="<?php echo $json['category_id']; ?>"  Selected="selected"><?php echo $json['name']; ?></option>');
        
        }
        error: function(){
            alert('error');
        }       
    });

}
</script>

Here is the controller function that receives the json request

    public function getSeries($brand){
$brand_id = $_POST['brand_id'];
$json = array();
$categories_2 = $this->model_catalog_category->gettoner($brand_id);

    foreach ($categories_2 as $category_2) {

        $json = array(
            'category_id' => $category_2['category_id'],
            'name'        => $category_2['name'],
        );


}
    $this->response->addHeader('Content-Type: application/json');
    $this->response->setOutput(json_encode($json));


    }
</div>
  • 写回答

1条回答 默认 最新

  • local-host 2016-04-21 12:45
    关注

    Here the problem may be caused by the folowing function: $this->model_catalog_category->gettoner($brand_id);

    Check to see whether the function returns a value in every situation. If not, simply return any value.Your dropdown menu will appear.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据