开发者

AJAX and MVC mixed

开发者 https://www.devze.com 2023-02-27 08:13 出处:网络
I need to have a dropdown that appears after I select the first dropdown, say the first dropdown is categories the second is subcategories. Here is what I have and for some reason its not working at a

I need to have a dropdown that appears after I select the first dropdown, say the first dropdown is categories the second is subcategories. Here is what I have and for some reason its not working at all

My view

  <td>Categories</td>
  <td><select class="categories" name="category_id">
      <option value="0" selected="selected">Select a Category</option>
      <?php foreach ($categories as $category) { ?>
      <option value="<?php echo $category['category_id']; ?>"><?php echo $category['name']; ?></option>
      <?php } ?>
    </select></td>
</tr>
<tr class="dont_show">

</tr>

My jquery

$(document).ready(function(){
    $('.categories').change(function() {
        var my_location = window.location.pathname.replace('admin/', '');
        $('.dont_show').load(my_location + '?route=module/cart/ajax_sub&category_id=35');   
    });
});

My php which is located in the class

public function ajax_sub(){
    $this->load->model('catalog/category');
    $sub_categories = $this->model_catalog_category->getCatego开发者_StackOverflowries($_GET['category_id']);
    $data = "<td>Subcategories</td>";
    $data += '<td><select name="category_id">';
    $data += '<option value="0" selected="selected">Select a Subcategory</option>';
    foreach ($sub_categories as $sub_category){
        $data +=  '<option value=' . $sub_category['sub_category_id'] . '">'. $sub_category['name'] . '</option>';
    }
    $data +=     '</select></td>';
    print $data;
}

It gets in here but always returns 0 .I tried rendering a view like this

    $this->template = 'default/template/module/ajax_sub.tpl';
    $this->render();

and putting my td in there but still no.any ideas what I am doing wrong.


Use .= instead of += for string contatenation in your php code.

0

精彩评论

暂无评论...
验证码 换一张
取 消