CodeIgniter Forums
Dynamic Dependent Select Box AJAX - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Dynamic Dependent Select Box AJAX (/showthread.php?tid=57665)



Dynamic Dependent Select Box AJAX - El Forum - 04-01-2013

[eluser]Thiago Leao[/eluser]
Hi guys, I'm trying to dynamically popular.

Select category and subcategory appears registered

Produto (Controller)

Code:
public function ajax_call() {
    
  $data['id'] = $this->input->post('id');
        $data = $this->produto_model->get_subcategoria($id);
            
        print form_dropdown('city',$data);
    
    }

Produto_model (MODEL)

Code:
function get_categoria() {
   $query = $this->db->query("SELECT * FROM categoria");
    
   if ($query->num_rows > 0) {
    return $query->result();
   }
  }
  
  function get_subcategoria($id) {
   $query = $this->db->query("SELECT FROM subcategoria WHERE id_categoria = '{$id}'");
    
   if ($query->num_rows > 0) {
    return $query->result();
   }
  }

VIEW

Code:
$(document).ready(function(){
$(".country").change(function()
{
  var id=$(this).val();
  var dataString = 'id='+ id;
  $.ajax
  ({
   type: "POST",
   url: "<? echo base_url(); ?>admin/produto/ajax_call",
   data: dataString,
   cache: false,
   success: function(html)
   {
   $(".city").html(html);
   }
  });

});
});


<select name="country" class="styled" class="country">
                <option selected="selected">Selecione uma categoria</option>
                &lt;? foreach($categoria as $listcategoria): ?&gt;
                    <option value="&lt;?=$listcategoria-&gt;id_categoria; ?&gt;">&lt;?=$listcategoria->categoria; ?&gt;</option>
                &lt;? endforeach; ?&gt;
            </select>          

            <select name="city" class="city">
                <option selected="selected">--Select City--</option>
            </select>

Could someone help me?
thanks!


Dynamic Dependent Select Box AJAX - El Forum - 04-01-2013

[eluser]TheFuzzy0ne[/eluser]
Your script sends the HTML for an entire <select>. So your dropdown ends up like this:
Code:
<select name="city" class="city">
    <select name="city" class="city">
        &lt;!-- options --&gt;
    </select>
</select>

You either need to just send the HTML for the <option> tags, or you need to completely replace your dropdown in your view.