CodeIgniter Forums
dynamic form_dropdown - Printable Version

+- CodeIgniter Forums (
+-- Forum: Using CodeIgniter (
+--- Forum: Libraries & Helpers (
+--- Thread: dynamic form_dropdown (/thread-73064.html)

dynamic form_dropdown - jennyvin16 - 03-15-2019


how to make a dynamic form_dropdown?
I have this in my view:
<td align="right" scope="row">Project Name</td>

$project_id = set_value('project_id', @$user[0]->DFLT_PROJECT_ID);

echo form_dropdown('project_id"'.' class="form-control', $projectlist, $project_id);

<span class="text-danger"><?php echo form_error('project_id'); ?></span>
<td align="right" scope="row">Stream Name</td>

$stream_id = set_value('stream_id', @$user[0]->DFLT_STREAM_ID);

echo form_dropdown('stream_id"'.' class="form-control', $streamlist, $stream_id);

<span class="text-danger"><?php echo form_error('stream_id'); ?></span>
I want the stream to be dependent on what the project_id will be. 

This is my controller 
    public function add_form_show($view) {
        $data['jira_hard_block'] = $this->settings_model->get_settings_by_type('JIRA_HARD_BLOCK');

        $session_user = get_cookie('auditme_cookie');
        $id = $this->user_model->get_id_by_user_name($session_user);
        $data['user'] = $this->user_model->get_user_by_id($id);
        $projectresult = $this->project_model->get_project_dropdown_list();
        $data['projectlist'] = $projectresult;
   $project_id = 1;
        $streamresult = $this->stream_model->get_stream_dropdown_list($project_id);
        $data['streamlist'] = $streamresult;

        $this->load->view($view, $data);

And this is my model:

    function get_stream_dropdown_list($project_id = '') {
    if ($project_id <> ''){
$this->db->order_by('STREAM_CODE', 'ASC');
$query = $this->db->get('AUDIT_STREAM');

        $result = $query->result();
if ($query->num_rows()>0) {
$data[''] = '- Please Select -';
            foreach($query->result() as $row) {
                $data[$row->ID] = $row->STREAM_NAME;
            return $data;
        return $result;

Help please.

RE: dynamic form_dropdown - php_rocs - 03-15-2019


We need more information. Is the project driven by user_id? Is a user in multiple projects? Is the project of a user known before the page is created?

RE: dynamic form_dropdown - jennyvin16 - 03-17-2019

The user has one project and stream defaulted. The user logs in and his details are the one used for details in the form. Once he enters this form, the default project and stream is selected. But if he changes the project from the dropdown, the stream dropdown should be updated with the available streams for this specific project.
This is already mapped in the database.

RE: dynamic form_dropdown - ciadmin - 03-17-2019

@jennyvin16 Forum policy, linked to at the top of each subforum, says that new users have their first few posts moderated, to prevent spam. Your reply would not show up until one of the volunteer moderators (most likely me) had a chance to check the moderation queue.

RE: dynamic form_dropdown - includebeer - 03-19-2019

To make dynamic form dropdow you need to write some JavaScript code to update the stream dropdown with new values when a value is selected in the project dropdown. So you need to attache an event on the project dropdown that will fire when the value is changed. You then need to get the new values to insert in the stream dropdown. You can get that list with an ajax request which will select the values in the database and return it to you javascript function.

RE: dynamic form_dropdown - jennyvin16 - 03-21-2019

Do you have code that I can use as basis? Or maybe a link I could use as reference?

Thank you so much!

RE: dynamic form_dropdown - includebeer - 03-27-2019

Sorry for the late reply. I will try to give you some sample code tomorrow if you still need it.

RE: dynamic form_dropdown - includebeer - 03-28-2019

(03-21-2019, 10:13 PM)jennyvin16 Wrote: Do you have code that I can use as basis? Or maybe a link I could use as reference?

I think this is a good simple example of what you're trying to do:

If you want to get the result from the database, make an ajax request to your application: