Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It's easier if you use <a href="http://jquery.com/" rel="nofollow noreferrer">jQuery</a>.</p> <p>Here is an example.</p> <p>In the main view you would have some like this:</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;script type="text/javascript" src="jquery.js"&gt;&lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;script&gt; $('#dropdown1').change(function(e) { $('#dropdown2').load('some_controller/some_function/'+this.value); }); &lt;/script&gt; &lt;select name="dropdown1" id="dropdown1"&gt; &lt;option value="1"&gt;option 1&lt;/option&gt; &lt;option value="2"&gt;option 2&lt;/option&gt; &lt;/select&gt; &lt;select name="dropdown2" id="dropdown2"&gt;&lt;/select&gt; &lt;/body&gt; </code></pre> <p>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.</p> <p>In the controller, you would have this:</p> <pre><code>&lt;?php class some_controller extends Controller { function some_function($id) { $this-&gt;load-&gt;model('some_model'); $data['options'] = $this-&gt;some_model-&gt;get_options($id); echo $this-&gt;load-&gt;view('view_dropdown2', $data, true); } } ?&gt; </code></pre> <p>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.</p> <p>And in the view:</p> <pre><code>&lt;?php foreach($options-&gt;result() as $option): ?&gt; &lt;option value="&lt;?php echo $option-&gt;id; ?&gt;"&gt;&lt;?php echo $option-&gt;title; ?&gt;&lt;/option&gt; &lt;?php endforeach; ?&gt; </code></pre> <p>You are just creating the options tags because the select tag was already created in the main view.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload