简体   繁体   中英

retrieve selected value in form_dropdown() and act on it in the same view

I am using codeigiter's form_dropdown() and would like to make the selection alter data farther down in the same view without submitting the form.

In the view php file:

echo form_dropdown('department_select',$options,'1');

<?php $test_list = $this->trainingmodel->get_dept_tests($deptselected); ?>

I would like $deptselected to reflect what the user has selected in the form_dropdown(). I would like this to update every time the user changes their dropdown selection but without submitting the form.

Things like the following would work if the form was submitted, but not before:

$deptselected = $_POST['department_select'];

or

$deptselected = $this->input->post('department_select');

There must be a way to do what I want using javascript, or onchange or the 4th input parameter to form_dropdown().

You can use the chained select jquery plugin http://www.appelsiini.net/projects/chained

A simple example taken from the website:

<select id="mark" name="mark">
       <option value="">--</option>
       <option value="bmw" selected>BMW</option>
       <option value="audi">Audi</option>
   </select>

<select id="series" name="series">
    <option value="--">--</option>
</select>

<select id="engine" name="engine">
    <option value="--">--</option>
</select>

<select id="transmission" name="transmission">
  <option value="--">--</option>
</select>

And on

$("#transmission").remoteChained({
    parents : "#engine",
    url : "/api/transmissions.json",
    depends : "#series"
});

You can set the ajax url to your Codeigniter controller that will serve the data to the view in json format.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM