php - Get data from the same form registration in Codeigniter

enter image description here

Based on the picture, this is for student registration form. Father and mother information would be in student table. But for guardian information would be at guardians table. In the form registration. I want to carry all information in "Father Information" when I choose "Father" at selection in "Guardian Information".

Controller

public function add($id = null) {
        
        check_permission(ADD);
     
        if ($_POST) {
            
            $school_id = $this->input->post('school_id');
            $class_id  = $this->input->post('class_id'); 
           
            $HQ  = $this->input->post('HQ');   


            $this->_prepare_student_validation();
            if ($this->form_validation->run() === TRUE) {
                $data = $this->_get_posted_student_data();
              

                if($this->input->post('lead_id') && $this->input->post('lead_id') != ''){  
                    $condition_lead = array();
                    $condition_lead['id'] = $this->input->post('lead_id');
                    $data_lead['is_converted'] = 1;
                    $this->db->update('student_leads', $data_lead, $condition_lead);
                }

                $insert_id = $this->student->insert('students', $data);
                
                if ($insert_id) {
                    $this->__insert_enrollment($insert_id);
                    // create_log('Has been added a student : '. $data['name']);    
                    $this->session->set_flashdata('filter_HQ','schools_mode');   
                    success($this->lang->line('insert_success'));
                    redirect('student/index/'.$this->input->post('class_id'));
                } else {
                    error($this->lang->line('insert_failed'));
                    redirect('student/add');
                }
            } else {

                error($this->lang->line('insert_failed'));
                $this->data['post'] = $_POST;
                $this->data['school_id'] = $_POST['school_id'];
            }
        }

        
        if($id){
            $this->data['lead'] = $this->student->get_list('student_leads', array('id' => $id,'is_converted' => 0), '', '', '', 'id', 'ASC');
            $this->data['lead_school'] = $this->student->get_list('schools', array('school_name' => $this->data['lead'][0]->preferred_school), 'id', '', '', 'id', 'ASC');
            if(count($this->data['lead']) == 0){
                error('No data for this lead or lead already converted to student');
                redirect('student/add');
            }
        }
        
        
        $class_id = $this->uri->segment(4);
        if(!$class_id){
          $class_id = $this->input->post('class_id');
        }

        $this->data['lead_id'] = $id;
        $this->data['class_id'] = $class_id;



        if($this->session->userdata('role_id') != SUPER_ADMIN && $this->session->userdata('role_id') != ADMIN_HQ && $this->session->userdata('role_id') != HUMAN_RESOURCE_HQ && 
        $this->session->userdata('role_id') != ACCOUNTING_HQ && $this->session->userdata('role_id') != FINANCE_HQ && $this->session->userdata('role_id') != MARKETING_HQ && 
        $this->session->userdata('role_id') != STUDENT_MANAGEMENT_HQ && $this->session->userdata('role_id') != CHIEF && $this->session->userdata('role_id') != EDUCATION_HQ){
            $this->data['students'] = $this->student->get_student_list($class_id, $school_id, $school->academic_year_id,$HQ);
        }else{
            if($this->input->post('HQ') != "" ){
                $this->data['students'] = $this->student->get_student_list($class_id, $school_id, $school->academic_year_id,$HQ);
            }
        }
       

        // $this->data['students'] = $this->student->get_student_list($class_id);
        $this->data['roles'] = $this->student->get_list('roles', array('status' => 1), '', '', '', 'id', 'ASC');
        
         
        $condition = array();
        $condition['status'] = 1;        
        if($this->session->userdata('role_id') != SUPER_ADMIN && $this->session->userdata('role_id') != ADMIN_HQ && $this->session->userdata('role_id') != HUMAN_RESOURCE_HQ && 
        $this->session->userdata('role_id') != ACCOUNTING_HQ && $this->session->userdata('role_id') != FINANCE_HQ && $this->session->userdata('role_id') != MARKETING_HQ && 
        $this->session->userdata('role_id') != STUDENT_MANAGEMENT_HQ && $this->session->userdata('role_id') != CHIEF && $this->session->userdata('role_id') != EDUCATION_HQ ){  
            
            $condition['school_id'] = $this->session->userdata('school_id');
            $this->data['classes'] = $this->student->get_list('classes', $condition, '','', '', 'id', 'ASC');
            $this->data['discounts'] = $this->student->get_list('discounts', $condition, '','', '', 'id', 'ASC');
            $this->data['guardians'] = $this->student->get_list('guardians', $condition, '','', '', 'id', 'ASC');
            $this->data['class_list'] = $this->student->get_list('classes', $condition, '','', '', 'id', 'ASC');
            $this->data['types']      = $this->student->get_list('student_types', $condition, '','', '', 'id', 'ASC');
        }
        
        $this->data['schools'] = $this->schools;
        $this->data['add'] = TRUE;
        $this->layout->title($this->lang->line('add'). ' | ' . SMS);
        $this->layout->view('student/index', $this->data);
    }

Model

  public function get_student_list($class_id = null, $school_id = null, $academic_year_id = null, $HQ = null ){
            
     
        $this->db->select('S.*, SC.school_name, E.roll_no, E.class_id, U.username, U.role_id,  C.name AS class_name, SE.name AS section');
        $this->db->from('enrollments AS E');
        $this->db->join('students AS S', 'S.id = E.student_id', 'left');
        $this->db->join('users AS U', 'U.id = S.user_id', 'left');
        $this->db->join('classes AS C', 'C.id = E.class_id', 'left');
        $this->db->join('sections AS SE', 'SE.id = E.section_id', 'left');
        $this->db->join('schools AS SC', 'SC.id = S.school_id', 'left');
        
        if($academic_year_id){
            $this->db->where('E.academic_year_id', $academic_year_id); 
        }
        if($class_id && $class_id != 'all'){
            $this->db->where('E.class_id', $class_id);
        }

        if($HQ != 'all' && $HQ != 'schools_mode' && $HQ != ''){
            $this->db->where('SC.group_by', $HQ);
        }
                
        if($this->session->userdata('role_id') == GUARDIAN){
           $this->db->where('S.guardian_id', $this->session->userdata('profile_id'));
        }
        
        if($this->session->userdata('role_id') == STUDENT){
           $this->db->where('S.id', $this->session->userdata('profile_id'));
        }
        
        if($this->session->userdata('role_id') != SUPER_ADMIN && $this->session->userdata('role_id') != ADMIN_HQ && $this->session->userdata('role_id') != HUMAN_RESOURCE_HQ && 
        $this->session->userdata('role_id') != ACCOUNTING_HQ && $this->session->userdata('role_id') != FINANCE_HQ && $this->session->userdata('role_id') != MARKETING_HQ && 
        $this->session->userdata('role_id') != STUDENT_MANAGEMENT_HQ && $this->session->userdata('role_id') != CHIEF && $this->session->userdata('role_id') != EDUCATION_HQ ){
            $this->db->where('S.school_id', $this->session->userdata('school_id'));
        }
        
        if($school_id){
            $this->db->where('S.school_id', $school_id); 
        } 
        $this->db->where('SC.status', 1);
        $this->db->order_by('E.roll_no', 'ASC');
       
        return $this->db->get()->result();
        
    }
    
    public function get_single_student($id,  $academic_year_id){
        
        $this->db->select('S.*,  SC.school_name, T.type, D.amount, D.title AS discount_title, SC.school_name, G.name as guardian, E.academic_year_id, E.roll_no, E.class_id, E.section_id, U.username, U.role_id, R.name AS role,  C.name AS class_name, SE.name AS section');
        $this->db->from('enrollments AS E');
        $this->db->join('students AS S', 'S.id = E.student_id', 'left');
        $this->db->join('users AS U', 'U.id = S.user_id', 'left');
        $this->db->join('roles AS R', 'R.id = U.role_id', 'left');
        $this->db->join('classes AS C', 'C.id = E.class_id', 'left');
        $this->db->join('sections AS SE', 'SE.id = E.section_id', 'left');
        $this->db->join('guardians AS G', 'G.id = S.guardian_id', 'left');
        $this->db->join('schools AS SC', 'SC.id = S.school_id', 'left');
        $this->db->join('discounts AS D', 'D.id = S.discount_id', 'left');
        $this->db->join('student_types AS T', 'T.id = S.type_id', 'left');
        $this->db->where('S.id', $id);
        $this->db->where('E.academic_year_id', $academic_year_id);

View

<div class="row">                  
                                    <div class="col-md-12 col-sm-12 col-xs-12">
                                        <h5  class="column-title"><strong><?php echo $this->lang->line('guardian_information'); ?>:</strong></h5>
                                    </div>
                                </div>
                                
                                <div class="row"> 
                                    <div class="col-md-3 col-sm-3 col-xs-12">
                                        <div class="item form-group">
                                            <label for="is_guardian"><?php echo $this->lang->line('is_guardian'); ?> <span class="required">*</span></label>
                                            <select  class="form-control col-md-7 col-xs-12 quick-field" name="is_guardian" id="is_guardian" required="required" onchange="check_guardian_type(this.value);">
                                                <option value="">--<?php echo $this->lang->line('select'); ?>--</option>
                                                <option value="father" <?php echo isset($post['father_name']) && $post['father_name'] == 'father_name' ?  'selected="selected"' : ''; ?>><?php echo $this->lang->line('father'); ?></option>
                                                <option value="mother" <?php echo isset($post['is_guardian']) && $post['is_guardian'] == 'mother_name' ?  'selected="selected"' : ''; ?>><?php echo $this->lang->line('mother'); ?></option>
                                                <option value="other" <?php echo isset($post['is_guardian']) && $post['is_guardian'] == 'other' ?  'selected="selected"' : ''; ?>><?php echo $this->lang->line('other'); ?></option>
                                                <option value="exist_guardian" <?php echo isset($post['is_guardian']) && $post['is_guardian'] == 'exist_guardian' ?  'selected="selected"' : ''; ?>><?php echo $this->lang->line('guardian_exist'); ?></option>
                                            </select>
                                            <div class="help-block"><?php echo form_error('is_guardian'); ?></div>
                                        </div>
                                    </div>
                                    
                                    <div class="fn_existing_guardian <?php if(isset($post['is_guardian']) && $post['is_guardian'] == 'exist_guardian'){'';}else{ echo 'display'; } ?>">
                                        <div class="col-md-3 col-sm-3 col-xs-12"> 
                                            <div class="item form-group">
                                                <label for="guardian_id"><?php echo $this->lang->line('guardian'); ?> <span class="required">*</span></label>
                                                <select  class="form-control col-md-7 col-xs-12 quick-field" name="guardian_id" id="add_guardian_id" onchange="get_guardian_by_id(this.value);">
                                                    <option value="">--<?php echo $this->lang->line('select'); ?>--</option>
                                                    <?php foreach($guardians as $obj){ ?>
                                                        <option value="<?php echo $obj->id; ?>" <?php echo isset($post['guardian_id']) && $post['guardian_id'] == $obj->id ?  'selected="selected"' : ''; ?>><?php echo $obj->name; ?></option>
                                                    <?php } ?>
                                                </select>
                                                <div class="help-block"><?php echo form_error('guardian_id'); ?></div>
                                            </div>
                                        </div>                                  
                                    </div>
                                                                        
                                    <div class="col-md-3 col-sm-3 col-xs-12">
                                        <div class="item form-group">
                                            <label for="relation_with"><?php echo $this->lang->line('relation_with_guardian'); ?> </label>
                                            <input  class="form-control col-md-7 col-xs-12"  name="relation_with"  id="add_relation_with" value="<?php echo isset($post['relation_with']) ?  $post['relation_with'] : ''; ?>" placeholder="<?php echo $this->lang->line('relation_with_guardian'); ?>" type="text">
                                            <div class="help-block"><?php echo form_error('relation_with'); ?></div>
                                        </div>
                                    </div> 
                                </div> 
                                   
                                
                                <div class="<?php echo ($post['is_guardian']) && $post['is_guardian'] != 'exist_guardian' ? '' :'display'; ?> fn_except_exist"> 
                                    <div class="row"> 

                                        <div class="col-md-3 col-sm-3 col-xs-12">
                                            <div class="item form-group">
                                                <label for="gud_name"><?php echo $this->lang->line('name'); ?> <span class="required">*</span></label>
                                                <input  class="form-control col-md-7 col-xs-12"  name="gud_name"  id="add_gud_name" value="<?php echo isset($post['gud_name']) ?  $post['gud_name'] : ''; ?>" placeholder="<?php echo $this->lang->line('name'); ?>" required="required" type="text">
                                                <div class="help-block"><?php echo form_error('gud_name'); ?></div>
                                            </div>
                                        </div>
                                        <div class="col-md-3 col-sm-3 col-xs-12">
                                            <div class="item form-group">
                                                <label for="gud_phone"><?php echo $this->lang->line('phone'); ?> <span class="required">*</span></label>
                                                <input  class="form-control col-md-7 col-xs-12"  name="gud_phone"  id="add_gud_phone" value="<?php echo isset($post['gud_phone']) ?  $post['gud_phone'] : ''; ?>" placeholder="<?php echo $this->lang->line('phone'); ?>" required="required" type="text">
                                                <div class="help-block"><?php echo form_error('phone'); ?></div>
                                            </div>
                                        </div>
                                        <div class="col-md-3 col-sm-3 col-xs-12">
                                            <div class="item form-group">
                                                <label for="gud_email"><?php echo $this->lang->line('email'); ?><span class="required">*</span> </label>
                                                <input  class="form-control col-md-7 col-xs-12"  name="gud_email"  id="add_gud_email" value="<?php echo isset($post['gud_email']) ?  $post['gud_email'] : ''; ?>" placeholder="<?php echo $this->lang->line('email'); ?>" required="email" type="email">
                                                <div class="help-block"><?php echo form_error('gud_email'); ?></div>
                                            </div>
                                        </div>
                                        <div class="col-md-3 col-sm-3 col-xs-12">
                                            <div class="item form-group">
                                                <label for="gud_profession"><?php echo $this->lang->line('profession'); ?></label>
                                                <input  class="form-control col-md-7 col-xs-12"  name="gud_profession"  id="add_gud_profession" value="<?php echo isset($post['gud_profession']) ?  $post['gud_profession'] : ''; ?>" placeholder="<?php echo $this->lang->line('profession'); ?>"  type="text">
                                                <div class="help-block"><?php echo form_error('gud_profession'); ?></div>
                                            </div>
                                        </div>                                   
                                        <div class="col-md-3 col-sm-3 col-xs-12">
                                            <div class="item form-group">
                                                <label for="gud_religion"><?php echo $this->lang->line('religion'); ?> </label>
                                                <input  class="form-control col-md-7 col-xs-12"  name="gud_religion"  id="add_gud_religion" value="<?php echo isset($post['gud_religion']) ?  $post['gud_religion'] : ''; ?>" placeholder="<?php echo $this->lang->line('religion'); ?>" type="text">
                                                <div class="help-block"><?php echo form_error('gud_religion'); ?></div>
                                            </div>
                                        </div>
                                        <div class="col-md-3 col-sm-3 col-xs-12">
                                            <div class="item form-group">
                                                <label for="gud_national_id"><?php echo $this->lang->line('national_id'); ?></label>
                                                <input  class="form-control col-md-7 col-xs-12"  name="gud_national_id"  id="add_gud_national_id" value="<?php echo isset($post['gud_national_id']) ?  $post['gud_national_id'] : ''; ?>" placeholder="<?php echo $this->lang->line('national_id'); ?>"  type="text">
                                                <div class="help-block"><?php echo form_error('gud_national_id'); ?></div>
                                            </div>
                                        </div>
                                        <div class="col-md-3 col-sm-3 col-xs-12">
                                            <div class="item form-group">
                                                <label for="gud_username"><?php echo $this->lang->line('username'); ?><span class="required">*</span></label>
                                                <input  class="form-control col-md-7 col-xs-12"  name="gud_username"  id="add_gud_username" value="<?php echo isset($post['gud_username']) ?  $post['gud_username'] : ''; ?>" placeholder="<?php echo $this->lang->line('username'); ?>"  type="text" required="required">
                                                <div class="help-block"><?php echo form_error('gud_username'); ?></div>
                                            </div>
                                        </div>                                        

                                    </div>
                                    
                                    <div class="row">    
                                        <div class="col-md-6 col-sm-6 col-xs-12">
                                            <div class="item form-group">
                                                <label for="gud_present_address"><?php echo $this->lang->line('present'); ?> <?php echo $this->lang->line('address'); ?></label>
                                                <textarea  class="form-control col-md-7 col-xs-12 textarea-4column"  name="gud_present_address"  id="add_gud_present_address" placeholder="<?php echo $this->lang->line('present'); ?> <?php echo $this->lang->line('address'); ?>"><?php echo isset($post['gud_present_address']) ?  $post['gud_present_address'] : ''; ?></textarea>
                                                <div class="help-block"><?php echo form_error('gud_present_address'); ?></div>
                                            </div>
                                        </div>

                                        <div class="col-md-6 col-sm-6 col-xs-12">
                                            <div class="item form-group">
                                                <label for="gud_permanent_address"><?php echo $this->lang->line('permanent'); ?> <?php echo $this->lang->line('address'); ?></label>
                                                <textarea  class="form-control col-md-7 col-xs-12 textarea-4column"  name="gud_permanent_address"  id="add_gud_permanent_address" placeholder="<?php echo $this->lang->line('permanent'); ?> <?php echo $this->lang->line('address'); ?>"><?php echo isset($post['gud_permanent_address']) ?  $post['gud_permanent_address'] : ''; ?></textarea>
                                                <div class="help-block"><?php echo form_error('gud_permanent_address'); ?></div>
                                            </div>
                                        </div>  
                                    </div>
                                    
                                    <div class="row">
                                        <div class="col-md-6 col-sm-6 col-xs-12">
                                            <div class="item form-group">
                                                <label for="other_info"><?php echo $this->lang->line('other_info'); ?> </label>
                                                <textarea  class="form-control col-md-7 col-xs-12 textarea-4column"  name="gud_other_info"  id="add_gud_other_info" placeholder="<?php echo $this->lang->line('other_info'); ?>"><?php echo isset($post['gud_other_info']) ?  $post['gud_other_info'] : ''; ?></textarea>
                                                <div class="help-block"><?php echo form_error('gud_other_info'); ?></div>
                                            </div>
                                        </div>                                        
                                    </div>
                                    
                                </div>

Answer

Solution:

View:-

<form>
    <select name="guardian" id="guardian">
        <option value="">-- Select Guardian Name -- </option>
        <option value="1">Father</option>
        <option value="2">Mother</option>                               
    </select>
    EMail-Id:<input name="email" type="text" value="" id="email">
 </form>

JS Code:-

<script>
    $(document).ready(function() {
        $("#guardian").change(function() {
                var id = $(this).val();
               
                $.ajax({
                    url: '<?php echo base_url().'Controller/method_name';?>',
                    type: 'post',
                    data: {id:id},
                    success: function(response) {

                      var result = $.parseJSON(response);
                      $("#email").val(result.email);
                         }
            });
        });
    });
</script>

Controller Code:-

<?php
    public function method_name(){
    
        $id= $this->input->post('id');
        $where="id=$id";
        $table="table_name";
        $data = $this->db->where($where)->get($table)->row_array();

       echo json_encode($data);
        }
?>

Source