php – Select ajax with an app built using CodeIgniter-ThrowExceptions

Exception or error:

I have a company_model and a controller. I also have 3 corresponding tables in a database for where the data is saved and read from. The goal of this is to manage companies linked to sub holdings and the corresponding directors of the sub holdings.

When I add a new company, the Sub holding and Director fields work perfectly. My issue is when editing an already saved company, the corresponding Director field is not populated or populating. I have been trying to find the issue for sometime now, I am not receiving any console errors or Network XHR when checking with Chrome. I know this is going to be a simple fix, I can not see it or find the issue. Any advice or pointers would be greatly appreciated

The Database structure is as follows:

Table 1 is for the director information (director_id, director_name, director_subholding_id)



Table 2 is for subholding information (subholding_id, subholding_name) 



Table 3 is for company information (company_id, company_name, ceo_name, company_subholding_id, 
     company_director_id)

Company_Model

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Company_model extends CI_Model{

function get_subholding(){
    $query = $this->db->get('subholding');
    return $query;  
}

function get_director($subholding_id){
    $query = $this->db->get_where('director', array('director_subholding_id' => $subholding_id));
    return $query;
}

function save_company($company_name,$subholding_id,$director_id,$ceo_name){
    $data = array(
        'company_name' => $company_name,
        'ceo_name' => $ceo_name,
        'company_subholding_id' => $subholding_id,
        'company_director_id' => $director_id 
    );
    $this->db->insert('company',$data);
}

function get_company(){
    $this->db->select('company_id,company_name,ceo_name,subholding_name,director_name');
    $this->db->from('company');
    $this->db->join('subholding','company_subholding_id = subholding_id','left');
    $this->db->join('director','company_director_id = director_id','left'); 
    $query = $this->db->get();
    return $query;
}

function get_company_by_id($company_id){
    $query = $this->db->get_where('company', array('company_id' =>  $company_id));
    return $query;
}

function update_company($company_id,$company_name,$subholding_id,$director_id,$ceo_name){
    $this->db->set('company_name', $company_name);
    $this->db->set('ceo_name', $ceo_name);
    $this->db->set('company_subholding_id', $subholding_id);
    $this->db->set('company_director_id', $director_id);
    $this->db->where('company_id', $company_id);
    $this->db->update('company');
}

//Delete Product
function delete_company($company_id){
    $this->db->delete('company', array('company_id' => $company_id));
}

Company Controller

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Company extends CI_Controller {
function __construct(){
    parent::__construct();
    $this->load->model('Company_model','company_model');
    $this->load->library('session');
}

function index(){
    $data['companies'] = $this->company_model->get_company();
    $this->load->view('company_list_view',$data);
}

// add new company
function add_new(){
    $data['subholding'] = $this->company_model->get_subholding()->result();
    $this->load->view('add_company_view', $data);
}

// get sub category by category_id
function get_director(){
    $subholding_id = $this->input->post('id',TRUE);
    $data = $this->company_model->get_director($subholding_id)->result();
    echo json_encode($data);
}

//save company to database
function save_company(){
    $company_name   = $this->input->post('company_name',TRUE);
    $subholding_id  = $this->input->post('subholding',TRUE);
    $director_id = $this->input->post('director',TRUE);
    $ceo_name   = $this->input->post('ceo_name',TRUE);
    $this->company_model->save_company($company_name,$subholding_id,$director_id,$ceo_name);
    $this->session->set_flashdata('msg','<div class="alert alert-success">Company Saved</div>');
    redirect('company');
}

function get_edit(){
    $company_id = $this->uri->segment(3);
    $data['company_id'] = $company_id;
    $data['subholding'] = $this->company_model->get_subholding()->result();
    $get_data = $this->company_model->get_company_by_id($company_id);
    if($get_data->num_rows() > 0){
        $row = $get_data->row_array();
        $data['director_id'] = $row['company_director_id'];
    }
    $this->load->view('edit_company_view',$data);
}

function get_data_edit(){
    $company_id = $this->input->post('company_id',TRUE);
    $data = $this->company_model->get_company_by_id($company_id)->result();
    echo json_encode($data);
}

//update company to database
function update_company(){
    $company_id     = $this->input->post('company_id',TRUE);
    $company_name   = $this->input->post('company_name',TRUE);
    $subholding_id  = $this->input->post('subholding',TRUE);
    $director_id = $this->input->post('director',TRUE);
    $ceo_name   = $this->input->post('ceo_name',TRUE);
    $this->company_model- 
>update_company($company_id,$company_name,$subholding_id,$director_id,$ceo_name);
    $this->session->set_flashdata('msg','<div class="alert alert-success">Company Updated</div>');
    redirect('company');
}

//Delete Company from Database
function delete(){
    $company_id = $this->uri->segment(3);
    $this->company_model->delete_company($company_id);
    $this->session->set_flashdata('msg','<div class="alert alert-success">Company Deleted</div>');
    redirect('company');
}

Edit Company View

<!DOCTYPE html>
<html>
<head>
<title>Edit Company</title>
<link href="<?php echo base_url().'assets/css/bootstrap.css'?>" rel="stylesheet" type="text/css">
</head>
<body>
<div class="container">

  <div class="row justify-content-md-center">
    <div class="col col-lg-6">
        <h3>Edit Company:</h3>

        <form action="<?php echo site_url('company/update_company');?>" method="post">

            <div class="form-group">
                <label>Company</label>
                <input type="text" class="form-control" name="company_name" placeholder="Company 
Name" required>
            </div>

            <div class="form-group">
                <label>Subholding</label>
                <select class="form-control subholding" name="subholding" required>
                    <option value="">No Selected</option>
                    <?php foreach($subholding as $row):?>
               <option value="<?php echo $row->subholding_id;?>"><?php echo $row->subholding_name;?></option>
                    <?php endforeach;?>
                </select>
            </div>

            <div class="form-group">
                <label>Director</label>
                <select class="form-control director" name="director" required>
                    <option value="">No Selected</option>

                </select>
            </div>

            <div class="form-group">
                <label>CEO</label>
                <input type="text" class="form-control" name="ceo_name" placeholder="CEO Name" 
required>
            </div>

            <input type="hidden" name="company_id" value="<?php echo $company_id?>" required>
            <button class="btn btn-success" type="submit">Update Company</button>

        </form>
    </div>
  </div>

</div>
<script type="text/javascript" src="<?php echo base_url().'assets/js/jquery-3.3.1.js'?>"></script>
<script type="text/javascript" src="<?php echo base_url().'assets/js/bootstrap.js'?>"></script>
<script type="text/javascript">
    $(document).ready(function(){
        //call function get data edit
        get_data_edit();

        $('.subhodling').change(function(){ 
            var id=$(this).val();
            var director_id = "<?php echo $director_id;?>";
            $.ajax({
                url : "<?php echo site_url('company/get_director_id');?>",
                method : "POST",
                data : {id: id},
                async : true,
                dataType : 'json',
                success: function(data){

                    $('select[name="director"]').empty();

                    $.each(data, function(key, value) {
                        if(director_id==value.director_id){
                            $('select[name="director"]').append('<option value="'+ value.director_id 
+'" selected>'+ value.director_name +'</option>').trigger('change');
                        }else{
                            $('select[name="director"]').append('<option value="'+ value.director_id 
+'">'+ value.director_name +'</option>');
                        }
                    });

                }
            });
            return false;
        }); 

        //load data for edit
        function get_data_edit(){
            var company_id = $('[name="company_id"]').val();
            $.ajax({
                url : "<?php echo site_url('company/get_data_edit');?>",
                method : "POST",
                data :{company_id :company_id},
                async : true,
                dataType : 'json',
                success : function(data){
                    $.each(data, function(i, item){
                        $('[name="company_name"]').val(data[i].company_name);

$('[name="subholding"]').val(data[i].company_subholding_id).trigger('change');
                        $('[name="director"]').val(data[i].company_director_id).trigger('change');
                        $('[name="ceo_name"]').val(data[i].ceo_name);
                    });
                }

            });
        }

    });
</script>
</body>
</html>
How to solve:

In your view, the select field for the Director is:

<select class="form-control director" name="directore" required>

Everywhere else in your view, controller and model, you’re expecting a variable named director which is not really there, so when you use $this->input->post('director') you’re actually getting a NULL value which explains why your updates are not working.

A simple fix would be to change your select to

<select class="form-control director" name="director" required>

and you should be ok

Leave a Reply

Your email address will not be published. Required fields are marked *