21 January 2021

How to use ajax properly in Codeigniter?

Use ajax properly in Codeigniter


Paste In view


<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script  src="<?php echo base_url(); ?>assets/js/custom.js" ></script>
<script>
$(document).ready(function(){
$(".ajax_bt").click(function(e) {
e.preventDefault();

    var person = $('#person').val();
    var phone = $('#phone').val();
    var email = $('#email').val();
    var password = $('#password').val();
//alert(person+''+''+phone+''+email+''+password);

  if(person != '' && phone != '' && email != '' && password != ''){
  
      $.ajax({
        url: "<?php echo base_url('index.php/Welcome/insert_data_with_ajax'); ?>",
        data: {
'person':person,
'phone':phone,
'email':email,
'password':password
}, // change this to send js object
        type: "post",
        success: function(data){
           //document.write(data); just do not use document.write
           console.log(data);
   $('#ajax_response').html(data);
   
        },
        error:function(data){
           //document.write(data); just do not use document.write
           console.log(data);
   $('#ajax_response').html('<p style="color:Red; margin:0px">Data not inserted...</p>');
   
        }
      });
       
    }
else{
$('#ajax_response').html('<p style="color:Red; margin:0px">Please fill all fields...</p>');
}


});
});
</script>



Paste In Controller


class Welcome extends CI_Controller {

 public function __construct()
    {
        parent::__construct();
        
        $this->load->library('session');
        $this->load->helper('html');
        $this->load->helper('url');
        $this->load->helper('form');
        $this->load->helper('file');
        $this->load->library('form_validation');
      
        $this->load->model('Customer_model');

       
    }


public function insert_data_with_ajax() {
$person = $this->input->post('person');
$phone = $this->input->post('phone');
$email = $this->input->post('email');
$password = $this->input->post('password');
$query = $this->Customer_model->register_user($person,$phone,$email,$password);
if($this->db->affected_rows() > 0){
 
echo 'Data inserted successfully';
 
}
else{  
echo 'Data Not inserted';
die();
}

public function ajax()
{
if ($this->session->userdata('ID') == FALSE)
        {
            
redirect('register'); //if session is not there, redirect to login page
        }
$data['main_content']='ajax';
$this->load->view('template',$data);  
}
 
}

Paste In Model

class Customer_model extends CI_Model {

    public function __construct()
    {
        $this->load->database();
        $this->load->helper('url');
    }


function update_user($user_id,$name,$phone,$email,$password){
$data = array(
'name' => $name,
'phone' => $phone,
'email ' => $email,
'password ' => $password
);
$this->db->update('login_form', $data);
$this -> db -> where('ID', $user_id);
}

}

No comments:

Post a Comment

How to create youtube videos slider with play and pause option in wordpress?

Create youtube videos slider with play and pause option in wordpress youtube videos slider Use this shortcode:- [punjab_today] function my_...