Welcome Guest, Not a member yet? Register   Sign In
Populate dropdown based on another dropdown selection using jquery
#3

[eluser]moriokasan[/eluser]
You are perfectly right, thank you! I resolved my issue today and I wanted to post the solution here, anyway, for others. It seems that a rested brain is a lot better than a sleepy one. So here is the solution that works (it is just a simple example)

Controller:
Code:
function index()
    {
        //check session
                $this->load->library('Mysession');
                if ($this->mysession->isInactive($this)) return;

                $this->load->model('crud2_model');

                if($query2 = $this->crud2_model->select_all('select p.id, p.name from expenses_providers p order by p.name asc'))
                {
                        $data['providers'] = $query2;
                }
                else
                {
                        $data['providers'] = array();
                }
                

                
        $this->load->view('test/dropdown', $data);
    }
        
        function getproducts()
    {
        //check session
                //$this->load->library('Mysession');
                //if ($this->mysession->isInactive($this)) return;
                
                

                $this->load->model('crud_model');

                $query2 = $this->crud_model->select_all_for_parentid('expenses_products', 'providerid', $this->uri->segment(4));
                
                
                
                foreach($query2 as $result)
                {
                        echo "<option value=".$result->id.">".$result->name."</option>";
                }
                
    }


Then the view looks like this

Code:
[removed]

                        function refresh_products(){
                                var selected_provider = $('#providers option:selected').val();
                                    
                                $('#products').load("/test/dropdown/getproducts/"+selected_provider);
                                
                        }

                        $(document).ready(function(){
                                refresh_products();
                                $('#providers').change(refresh_products);
                        });

                [removed]
        &lt;/head&gt;


        &lt;body&gt;
                <h1> testing jquery dropdown in cascade</h1>

                &lt;input type="text" id="debug"&gt;Debug text&lt;/input&gt;&lt;br></br>

                <select id="providers" name="providers">
                &lt;?php if(isset($providers)) : foreach($providers as $provider) : ?&gt;
                        <option value=&lt;?php echo $provider->id; ?&gt;>
                                &lt;?php echo $provider->name; ?&gt;
                        </option>                        
                &lt;?php endforeach; endif; ?&gt;
                </select>

                <select id="products" name="products">
                </select>
        &lt;/body&gt;

I hope this helps someone!

All the best!

Marius


Messages In This Thread
Populate dropdown based on another dropdown selection using jquery - by El Forum - 06-15-2011, 02:17 PM



Theme © iAndrew 2016 - Forum software by © MyBB