#1
Hello, Needed some help on this problem and any help rendered is most appreciated. When user selected the required client name (first dropdown list), the selected value must be used to derive the items in the second dropdown list for the user to select. form.php view file: ... echo form_label('Client Name'); echo form_dropdown('client_id', $client_options, $client_id); //First Dropdown List echo form_label('Survey Name'); echo form_dropdown('survey_frm_id', $cln_survey_options, $survey_frm_id); //Second Dropdown List Pet_forms.php controller file: function form() { $this->module('trongate_security'); $this->trongate_security->_make_sure_allowed(); $update_id = (int) segment(3); $submit = post('start'); if (($submit == '') && ($update_id>0)) { $data = $this->_get_data_from_db($update_id); } else { $data = $this->_get_data_from_post(); } if ($update_id>0) { $data['headline'] = 'Update Survey Entry Form'; $data['cancel_url'] = BASE_URL.'survey_forms/show/'.$update_id; } else { $data['headline'] = 'Survey Entry Form'; $data['cancel_url'] = BASE_URL.'survey_forms/manage'; if ($data['survey_frm_id']> 0) { redirect('survey_forms/start/'.$data['survey_frm_id']); } } $data['form_location'] = BASE_URL.'survey_forms/form/'.$update_id; $data['view_file'] = 'form'; $data['client_options'] = $this->_get_client_options($data['client_id']); $data['cln_survey_options'] = $this->_get_cln_survey_options($data['client_id'])); $this->template('admin', $data); } function _get_data_from_post() { $data['client_id'] = post('client_id', true); $data['survey_frm_id'] = post('survey_frm_id', true); return $data; } function _get_client_options($selected_client_id) { $rows = $this->model->get('client_name', 'clients'); if ($selected_client_id == '') { $options[''] = 'Select client...'; } foreach($rows as $row) { $options[$row->id] = $row->client_name; } return $options; } function _get_cln_survey_options($selected_client_id) { $sql = 'select distinct survey_forms.survey_frm_id, surveys.survey_name FROM survey_forms INNER JOIN surveys ON survey_forms.survey_frm_id = surveys.id WHERE survey_forms.client_id = '.$selected_client_id.' ORDER BY survey_forms.survey_frm_id'; $rows = ($selected_client_id == Null ? Null : $this->model->query($sql, 'object')); if ($selected_client_id == '') { $options[''] = 'Select survey...'; } if ($rows > 0) { foreach($rows as $row) { $options[$row->survey_frm_id] = $row->survey_name; } } return $options; } Regards
#2
Could you post your code in code tags beginning tag = code , ending tag /code. With Square brackets. [ ] Thanks, Dan
#3
Survey_forms.php controller file: form.php view file:
#4
Hi London Gypsy, This would not be possible in php since all the html is rendered on the server then pushed to the browser. You can do this using AJAX with some javascipt code however. examples : https://makitweb.com/auto-populate-dropdown-with-jquery-ajax/ https://stackoverflow.com/questions/48339141/use-ajax-response-to-update-select-option
#5
Hi London Gypsy, KeepCalm is right, this sort of thing happens in JavaScript. You can find a great tutorial by DC here https://youtu.be/lPtSUpNr_KQ It should have all the information you need on this subject ps. Not sure what you are doing with your code pastes but a quick tip is to use an online HTML entities convertor like this one https://www.online-toolz.com/tools/text-html-entities-convertor.php to paste between code-blocks.
#6
As recommended by Dafa, based on DC's video - https://www.youtube.com/watch?v=lPtSUpNr_KQ, I was able to get my first dropdown list to pass the selected value to my sql query to provide a list of values to my second dropdown list. DC's video was very detailed and clear! Not sure why the HTML Entities converter was not working for me. I pasted my php code to the Text box, click the Convert button, copied the result from the HTML Entities box and pasted into here between .