CodeIgniter Forums

Full Version: dependent dropdown is not working
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Unknown[/eluser]
i want to popualte two dropdown one is for compnay names and other is for model names.
actually i filled first dropdown from database.
but i want to fill second dropdown from database with onchange event on first dropdown.

Code:
this is script code
<?php //echo form_open("welcome"); ?>
<?php $cities['#'] = 'Please Select'; ?>

<label for="country">Truck </label>
&lt;?php echo form_dropdown('mkno', $countries, '#', 'id="country"'); ?&gt;<br />

<label for="city">Model </label>
&lt;?php echo form_dropdown('modno', $cities, '#', 'id="cities"'); ?&gt;<br />
<select id="cities" name="cities" >
&lt;input type="submit" name="submit" value="submit" &gt;


  [removed]
$('#cities, #f_city_label').hide();
$('#country').change(function(){
    var state_id = $('#country').val();
    if (state_id != ""){
        var post_url = "/index.php/welcome/get_cities" +state_id;
        $.ajax({
            type: "POST",
             url: post_url,
             success: function(cities) //we're calling the response json array 'cities'
              {
                $('cities').empty();
                $('#cities, #city_label').show();
                   $.each(cities,function(modno,modnm)
                   {
                    var opt = $('<option />'); // here we're creating a new select option for each group
                      opt.val(modno);
                      opt.text(modnm);
                      $('#cities').append(opt);
                });
               } //end success
         }); //end AJAX
    } else {
        $('#cities').empty();
        $('#cities, #city_label').hide();
    }//end if
}); //end change
[removed]
        

This is Model Code to Popualte second dropdown from database with selection of first dropdown

function get_cities_by_state($tree = null)
  {
        $this->db->select('modno,modnm');

        if($tree != NULL){
     $this->db->select('modno,modnm');
            $this->db->where('mkno', $state);
        $query = $this->db->get('mkmodel');
        $cities = array();

        if($query->result())
  {
            foreach ($query->result() as $city)
   {
                $cities[$city->modno] = $city->modnm;
            }
            return $cities;
        }
  else
  {
            return FALSE;
        }
    }

El Forum

[eluser]InsiteFX[/eluser]
Please wrap your code in code tags, no one will try to read your code looking like that.