Codeigniter is a MVC framework, which has its own functionality to insert a form data to mysql database table. Now by using ajax post method we will insert the codeigniter form data to mysql database table.
Now we will create a database table as branddetails, which looks as below.






CREATE TABLE IF NOT EXISTS `branddetails` (
  `brand_id` int(11) NOT NULL AUTO_INCREMENT,
  `brandname` varchar(50) NOT NULL,
  `dealername` varchar(50) NOT NULL,
  `emailid` varchar(50) NOT NULL,
  `wedaddress` varchar(50) NOT NULL,
  `city` varchar(50) NOT NULL,
  `contactno` varchar(50) NOT NULL,
  `state` varchar(50) NOT NULL,
  PRIMARY KEY (`brand_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;

After creating the table, now we will create a view to insert the
Form data to branddetails table.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <form id="branddet" name="branddet" method="post" role="form" >   
  <table width="952">
  <tr>
    <td colspan="4" align="center">&nbsp;</td>
  </tr>
  <tr>
    <td width="110">Brand Name</td>
    <td width="291"><input type="text" name="brand_name"></td>
    <td width="212">Email ID</td>
    <td width="319"><input type="text" name="emailid"></td>
  </tr>
  <tr>
    <td>Dealer Name</td>
    <td><input type="text" name="dealername"></td>
    <td>Web Address</td>
    <td><input type="text" name="webaddress"></td>
  </tr>
  <tr>
    <td>Address</td>
    <td><input type="text" name="address"></td>
    <td>State</td>
    <td><input type="text" name="state"></td>
  </tr>
  <tr>
    <td>Contact No</td>
    <td><input type="text" name="contactno"></td>
    <td>City</td>
    <td><input type="text" name="city"></td>
  </tr>
  <tr>
    <td>Fax</td>
    <td><input type="text" name="fax"></td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  
  <tr>
    <td colspan="4" >
    <div class="col-lg-offset-3 col-lg-9">
    <button type="button" name="submit" class="btn btn-info" onclick="branddetails()" >   Save  </button>
    <button  type="reset" name="reset"  class="btn btn-info">   Reset  </button>  <img src="<?php echo $baseurl;?>images/loaders/circular/035.gif" alt="" id="loading_pic" style="display:none"></div> </td>
  </tr>
</table>
</form>
</body>
</html>

After creating view, now let’s talk about Ajax post method, here I created a JavaScript function branddetails. Here Serialize () method is used to create URL encoded text string from input form values, and these values can be used in URL query string when making Ajax post request, now write ajax post method as below.

<script type="application/javascript">
 function branddetails()
 {
		Var paramstr=$("#branddet").serialize();
		$('#loading_pic').show();
		$.post("<?php echo base_url();?>dashboard/branddetailsinsert",
		paramstr, 
		function(data) 
		{
		$('#loading_pic').hide();
		 if(data == "Success")
		 {
		   $('#success_msg').show();
		  $('#success_msg').text(" Record saved successfully");
		 }
		});	
		
 }
 </script>

Now store encoded text string to variable paramstr, now use jquery ajax post method to send form request using url.
Create branddetailsinsert method under dashboard controller in codeigniter. Let’s look into the code once to insert the form data from ajax post method.

<?php
class Dashboard extends CI_Controller
  {
 public $data;
 	public function __construct()
					{
				//Core controller constructor
				parent::__construct();
				
				$this->load->model('dashboard/dashboardmodel');
			       }    
 public function branddetailsinsert()
 {
 
  $insertstatus=$this->dashboardmodel->insertbranddetials();
  if($insertstatus)
  {
  echo "Success";
  }
}
?>

In the above controller we are calling model class insertbranddetails() method to insert form data, as you know codeigniter uses model, view and controller architecture. Now let’s check model class methods in below code.

<?php
class Dashboardmodel extends CI_Model{
	public function insertbranddetials()
	{
	    
		 	   
		  $brandname=$this->input->post("brand_name");
		  $dealername=$this->input->post("dealername");
		  $emailid=$this->input->post("emailid");
		  $address=$this->input->post("address");
		  $webaddress=$this->input->post("webaddress");
		  $city=$this->input->post("city");
		  $contactno=$this->input->post("contactno");
		  $state=$this->input->post("state");
		  $fax=$this->input->post("fax");

		   $branddet = array(
		   'brand_name' => $brandname ,
		   'dealer_name' => $dealername ,
		   'email_id' =>   $emailid,
		   'Address' => $address ,
		   'website_addr' => $webaddress,
		   'city' => $city ,
		   'contact_number' => $contactno,
		   'state' => $state ,
		   'fax_number' => $fax
		    );
	        
	 $insertdet=$this->db->insert('branddetails', $branddet);
	 return $insertdet;
}
?>

As you know when array is passed into codeigniter insert function
It will execute the method and inserts data into branddetails database table, and returns the stored variable $insertdet to controller method, finally controller function check’s weather the data inserted using if condition ,if its inserts return result as “successful” else return “not successful”.
This returned value is passed to ajax post method success function and displays alert message as successful.

Thanks for reading this article.