• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
dynamic form_dropdown

#1
Hello,

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

$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>
</tr>
<tr>
<td align="right" scope="row">Stream Name</td>
<td>
<?php

$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>
</td>
</tr>
 
I want the stream to be dependent on what the project_id will be. 

This is my controller 
Code:
    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:

Code:
    function get_stream_dropdown_list($project_id = '') {
    if ($project_id <> ''){
      $this->db->where("proj_id",$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.
Reply

#2
@jennyvin16,

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?
Reply

#3
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.
Reply

#4
@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.
Reply

#5
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.
Test your translation files with Translation Tester
Reply

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

Thank you so much!
Reply

#7
Sorry for the late reply. I will try to give you some sample code tomorrow if you still need it.
Test your translation files with Translation Tester
Reply

#8
(03-21-2019, 11: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: https://stackoverflow.com/a/40641405
Demo: https://jsfiddle.net/6xtzt8k4/

If you want to get the result from the database, make an ajax request to your application: https://stackoverflow.com/a/51888211
Test your translation files with Translation Tester
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.