views:

161

answers:

2

Hi! I’m trying to link two dropdownlist depending on the value you select from the first dropdownlist. I don’t have any idea how to do this, so if you guys can point me in the right direction that would be great.

What I want to do is select a value from the first dropdownlist and depending on its value show some options in the second dropdownlist.

I try doing this:

1-when the “OnChange” event of the first dropdownlist occurs, I call a javascript that redirects to a method in a controller (for example: index/mainController/firstMethod/selectedValue).

2-the method in the controller using the url helper extracts the “selectedValue” to do a database search of the value of the second dropdownlist.

3-the same method loads the view (the one that have the ddl)

Doing this I lose the selected value of the first ddl.

I think this is not the right way to do it.

So, once again, I need some help.

Thanks for your time.

A: 

It sounds like you want AJAX. I'm not as up on this as I used to be, but the general procedure should go something like this:

When the user selects something in the first dropdown, it fires a javascript method. This method grabs that value, uses it to GET query a page via a XMLHttpRequest. The XMLHttpRequest has a callback, such that when the data comes back, you can populate the second dropdown with that data.

It's probably easiest (although somewhat odd) to have the PHP page that it calls just return the full HTML for the new dropdown. They you just have to do something like document.getElementById('secondDropdown').innerHTML = requestContent;.

zebediah49
A: 

It's easier if you use jQuery.

Here is an example.

In the main view you would have some like this:

<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
</head>
<body>
<script>
    $('#dropdown1').change(function(e) {
        $('#dropdown2').load('some_controller/some_function/'+this.value);
    });
</script>

<select name="dropdown1" id="dropdown1">
    <option value="1">option 1</option>
    <option value="2">option 2</option>
</select>

<select name="dropdown2" id="dropdown2"></select>
</body>

You are saying in the script that whenever the dropdown change its value, you call (in Ajax) a controller function with the id in its arguments. Don't forget to include jQuery file.

In the controller, you would have this:

<?php
class some_controller extends Controller {

    function some_function($id) {
        $this->load->model('some_model');
        $data['options'] = $this->some_model->get_options($id);
        echo $this->load->view('view_dropdown2', $data, true);
    }
}
?>

So, when this function is called, you call your model function with the dropdown id, and you echo the view with the options. You have to put the 3rd argument true, so that the view is returned as a var and not rendered.

And in the view:

<?php foreach($options->result() as $option): ?>
    <option value="<?php echo $option->id; ?>"><?php echo $option->title; ?></option>
<?php endforeach; ?>

You are just creating the options tags because the select tag was already created in the main view.

dmb