ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Refreshing a dropdown list when selecting another dropdown list.

August 18, 2008 4:44am

Subscribe [10]
  • #16 / Aug 18, 2008 10:06am

    theighost

    19 posts

    i re arranged the javascript which wasnt colored(ignored) for some reason…now i see the alert as index.php/state/-1

    it means it applies it.

    now i built a function in the controller that is named state() this function loads a view which name is state.php

    another function (which i apply in the url to test) is called state_dropdown() which loads the ajax script and has the connection not to the file state but to the function state that has the connection to the file state.

    I hope u got me.

    anyway the dropdown list remains empty!

  • #17 / Aug 19, 2008 2:24pm

    vendiddy

    33 posts

    You might have to wrap that jquery script in a $(document).ready(function { ... });

  • #18 / Nov 12, 2009 11:15am

    spmckee

    56 posts

  • #19 / Nov 12, 2009 12:34pm

    hugle

    289 posts

    hi,

    I have done something similar you are asking, using CI and jQuery,
    you can take a look here:
    http://demo3.invista.lt/search/page/1/loans/giving_and_requesting

    If you liek it, I can share the code 😊

  • #20 / Nov 12, 2009 1:30pm

    spmckee

    56 posts

    hi,

    I have done something similar you are asking, using CI and jQuery,
    you can take a look here:
    http://demo3.invista.lt/search/page/1/loans/giving_and_requesting

    If you liek it, I can share the code 😊

    I’ll bite. I’ve been trying to get something like that going and would love to see a working version.

  • #21 / Nov 14, 2009 12:19pm

    Bui Duc Long

    34 posts

    hi,

    I have done something similar you are asking, using CI and jQuery,
    you can take a look here:
    http://demo3.invista.lt/search/page/1/loans/giving_and_requesting

    If you liek it, I can share the code 😊

    hey mate, I like it :D

  • #22 / Nov 17, 2009 10:34am

    doob

    5 posts

    Maybe this can help you.

    View 1

    <select name="state" id="selectstate">
        <option value="1">State 1</option>
        <option value="2">State 2</option>
        <option value="3">State 3</option>
        <option value="4">State 4</option>
    </select>
    
    <div id="citieswrapper">
    <select name="city" id="selectcity">
        <option value="">- Select state</option>
    </select>
    </div>

    jQuery

    $("#selectstate").bind("change",function(){
    
        $("#citieswrapper").load("/ajax/get_city", {state_id: $(this).val()} );
    });

    Controller

    <?php
    
    class Ajax extends Controller {
    
        function Ajax()
        {
            parent::Controller();
            
            $data = array();
        }
        
        function index()
        {
            
        }
    
        function get_city()
        {
            // Load location model
            $this->load->model('location_model', 'location');
    
            // Which state to load?    
            $state_id = $this->input->post('state_id');
            
            $data['cities'] = $this->location->get_cities_in_state($state_id)->result();
            
            $this->load->view('ajax/city_list', $data);
        }
    
    }

    Model

    <?php
    
    class Location_Model extends Model 
    {
        function Location_Model()
        {
            parent::Model();
            
            $this->_city_table = "cities";
        }
    
        function get_cities_in_state($state_id)
        {
            $this->db->where('state_id', $state_id);
            $this->db->order_by('city_name');
            return $this->db->get($this->_city_table);
        }

    View: ajax/city_list.php

    <select name="city">
        <?php foreach($cities as $city) : ?>
            <option value="<?php echo $city->city_id; ?>"><?php echo $city->city_name; ?></option>
        <?php endforeach; ?>
    </select>

    DB-table:  cities

    id        state_id        city_name
    1        1                City 1
    2        1                City 2
    3        2                A city in state 2
    4        3                Another city in state 2
    5        4                City in state 4
    6        4                City two in state 4
  • #23 / Dec 04, 2009 11:24am

    alberta

    19 posts

    Hi,

    I have used the above code and it works fine. I have used it in form but problem is that when form is submitted, value returned by second drop down menu i.e filtered city is empty.

    Where is the problem ?

    Thanks

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases