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

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.

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;

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

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

function get_cities_by_state($tree = null)

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

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

El Forum

Please wrap your code in code tags, no one will try to read your code looking like that.