Here I am trying to make a form which enables admin to add a new company , i need to implement the form validation and after that insert data to table (company_details),here is what i have done, using codeigniter and ajax, I am not sure if there is other simple ways to do this process without using ajax , so if there is i would appreciate you help me. here is my code but now when i press create button notting actually happens, and i get "error :Failed to load resource: the server responded with a status of 500 (Internal Server Error). company:1"
Company.php Controller
<?php
class Company extends CI_Controller
{
function __construct()
{
parent::__construct();
if(!$this->session->userdata('admin'))
redirect('admin');
$this->load->model('company_model');
}
function index()
{
$data['company'] = $this->company_model->getCompanyDetails();
$this->load->view('admin/company', $data);
}
function validation()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('Cname' , 'Company Name' , 'required');
$this->form_validation->set_rules('shname' , 'Short Name' , 'required');
$this->form_validation->set_rules('regNo' , 'Registration No' , 'required|valid_regNo');
if($this->form_validation->run())
{
$this->company_model->create();
$array = array(
'succsee' => '<div class="alert alert success">Your Company Added Sucessfully</div>'
);
redirect('admin/company/');
}
else
{
$array = array(
'error' => true,
'Cname_error' => form_error('Cname'),
'shname_error' => form_error('shname'),
'regNo_error' => form_error('regNo'),
);
}
echo json_encode($array);
}
}
company.php view
<div class="companyAdd" style="display: none;">
<h2 class="heading">Add Company</h2>
<span action="<?php echo site_url('admin/staff/validate');?>" id="success_message"></span>
<form method="post" id="AddCompanyForm">
<div class="row">
<div class="form-group col-md-4">
<label for="email">Company Name</label>
<input type="text" class="form-control" id="Cname" placeholder="Name" name="Cname">
<span id="Cname_error" class="text-danger"></span>
</div>
<div class="form-group col-md-4">
<label for="pwd">Short Name</label>
<input type="text" class="form-control" id="shname" placeholder="Short Name" name="shname">
<span id="shname_error" class="text-danger"></span>
</div>
<div class="form-group col-md-4">
<label for="pwd">Registration No</label>
<input type="text" class="form-control" id="regNo" placeholder="01234567" name="regNo">
<span id="regNo_error" class="text-danger"></span>
</div>
<div class="col-md-12">
<div class="btn-section float-right">
<button class="btn btn-outline-primary" type="submit"><i class="fa fa-plus-circle" aria-hidden="true" id="Ccreat"></i> Create</button>
<button class="btn btn-danger float-right" id="cancelCompany" type="button" onClick="window.location.href=admin/company"><i class="fa fa-plus-circle" aria-hidden="true"></i> Cancel</button>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
This is my script tag in company.php view
<script>
$(document).ready(function(){
$('#AddCompanyForm').on('submit', function(event){
event.preventDefault();
$.ajax({
url:"<?php echo site_base();?>admin/company/validation",
method:"POST",
data:$(this).serialize(),
dataType:"json",
beforeSend:function(){
$('#Ccreat').attr('disabled', 'disabled');
},
success:function(data)
{
if(data.error)
{
if(data.Cname_error != '')
{
$('#Cname_error').html(data.Cname_error);
}
else
{
$('#Cname_error').html('');
}
if(data.shname_error != '')
{
$('#shname_error').html(data.shname_error);
}
else
{
$('#shname_error').html('');
}
if(data.regNo_error != '')
{
$('#regNo_error').html(data.regNo_error);
}
else
{
$('#regNo_error').html('');
}
}
if (data.success)
{
$('#success_message').html(data.success);
$('#Cname_error').html('');
$('#shname_error').html('');
$('#regNo_error').html('');
$('#AddCompanyForm')[0].reset();
}
$('#Ccreat').attr('disabled',false);
}
})
});
});
</script>
And here is Company_model.php
<?php
class Company_model extends CI_Model
{
//table name: company_details
function getCompanyDetails()
{
return $this->db->get('company_details')->result();
}
function create()
{
$arr['company_name'] = $this->input->post('Cname');
$arr['short_name'] = $this->input->post('shname');
$arr['registration_no'] = $this->input->post('regNo');
$this->db->insert('company_details',$arr);
}
}
This is my first time working with codeigniter and ajax , i would appreciate if someone can help me to solve it.