How to prepopulate a cascaded drop down and attachment on validation failure
Add this code at the end of view page
For country dropdown
<?php if (!empty(set_value('country_id'))) : ?> <script type="text/javascript"> var value = "<?php echo set_value('country_id'); ?>"; $('select[name="country_id"]').find('option[value="'+value+'"]').attr("selected",true); </script><?php endif; ?>
For state dropdown
<?php if (!empty(set_value('state_id'))) : ?> <script type="text/javascript"> var value = "<?php echo set_value('state_id'); ?>"; $('select[name="state_id"]').find('option[value="'+value+'"]').attr("selected",true); </script><?php endif; ?>
Follow the below points
1) For select
tag, use set_select()
, like this
<select name="myselect"> <option value="one" <?php echo set_select('myselect', 'one', TRUE); ?> >One</option> <option value="two" <?php echo set_select('myselect', 'two'); ?> >Two</option> <option value="three" <?php echo set_select('myselect', 'three'); ?> >Three</option></select>
2) If the form validation failed, call your ajax function to set second drop down.
3) For attachment, Whatever result of the form validation, first upload the image and keep the image path in session variable. After successful form validation, save the data into database and destroy the session variable.
edited >>>
**html**<?php if (isset($edit) && !empty($edit)) { //while editing $StateID = $edit['StateID']; $CountryID = $edit['CountryID'];} else { // while adding $StateID = set_value('StateID'); $CountryID = set_value('CountryID'); } ?> <div class="col-3"> <div class="form-group"> <label class="mb-0">Country</label> <select class="form-control Country" id="country" name="CountryID" data_state_value="<?php echo $StateID; ?>" > <option value="">Select Country</option> <?php foreach ($country as $row) { $selected = ($row->id == $CountryID) ? 'selected' : ''; echo '<option value="' . $row->id . '"' . $selected . '>' . $row->country_name . '</option>'; } ?> </select> <span class="text-danger font9"><?php echo form_error('CountryID'); ?></span> </div> </div> <div class="col-3"> <div class="form-group"> <label class="mb-0">State</label> <select class="form-control State" id="State" name="StateID"> </select> <span class="text-danger font9"><?php echo form_error('StateID'); ?></span> </div> </div>**script**
$(document).on('change', '.Country', function () { var country_id = $(this).val(); var state_id = $(this).attr('data_state_value'); url = $("body").attr('b_url'); $.ajax({ url: url + "Education/fetch_state", method: "POST", data: { country_id: country_id, state_id: state_id }, success: function (res) { var response = $.parseJSON(res); $('#State').html('<option value="">Select State</option>' + response.view); $('.State').trigger('change'); } });});$('.Country').trigger('change');
**controller** public function fetch_state() { $data = array(); if (isset($_POST['country_id']) && !empty($_POST['country_id'])) { $states = $this->Location_model->fetch_state($this->input->post('country_id')); // array of states $view = array(); if (!empty($states)) { foreach ($states as $val) { $selected = (isset($_POST['state_id']) && !empty($_POST['state_id']) && $_POST['state_id'] == $val['id']) ? 'selected' : ''; $view[] = "<option value='" . $val['id'] . "'" . $selected . ">" . $val['states_name'] . "</option>"; } $data['view'] = $view; $data['status'] = 0; } else { $data['status'] = 0; $data['view'] = '<option value="">No State Found</option>'; } } else { $data['status'] = 0; $data['view'] = '<option value="">Select State</option>'; } echo json_encode($data);}