Friday, 18 March 2016

how to create session using ajax in codeigniter

Create Session Using Ajax

We would discuss about to create seesion using codeigniter. Here we have to done on the function to do that codeigniter framework. The given fields like username, password, date of birth, email, phone number those are validation here.

Then we have to move instalization to create file in controller folder. Then given below coding to copy and paste there. That function name is add_register. In that function we do the form validation and store form data to the particular variable. Then it should pass to the library function.

 <?php  
 //controller  
       public function add_register()  
       {  
            //form validation  
           $this->form_validation->set_rules('user_name','Username','trim|required|xss_clean|is_unique[user.user_name]');  
           $this->form_validation->set_rules('dob','Date Of Birth','trim|required|xss_clean');  
           $this->form_validation->set_rules('user_email','Email','trim|required|valid_email|xss_clean|is_unique[user.user_email]');  
           $this->form_validation->set_rules('ph_no','Phone Number','trim|required|numeric|xss_clean');  
           $this->form_validation->set_rules('user_password','Password','trim|required|xss_clean');  
           if($this->input->is_ajax_request()) { $data['ajax']=TRUE; } else { $data['ajax']=FALSE; }  
           $data['response']='';  
           if($this->form_validation->run()===FALSE)  
           {  
                //show error if in validation  
                $data['reg_status']=FALSE;$data['response']='validation_fail';$data['status']=0;  
                $data['reg_message']=$this->form_validation->error_array();  
           }  
           else  
           {  
                //form data store in array and store in $register_data  
                $register_data=array('user_name'=>$this->input->post('user_name'),'user_email'=>$this->input->post('user_email'),'user_password'=>$this->input->post('user_password'),'dob'=>$this->input->post('dob'),'ph_no'=>$this->input->post('ph_no'));  
                //stored data is pass to user_register_data librery  
                $reg_data=$this->/*librery*/->user_register_data($register_data);  
                if($reg_data==TRUE)  
                {  
                     //show result here  
                     $data['mail_sent']=TRUE;$data['status']=1;$data['response']='success';  
                     $data['reg_message']='Registration Succesfull.<br/> Plesase Check your mail to Activate your account';  
                }  
                else  
                {  
                     $data['status']=2;$data['response']='success_mail_not_sent';  
                     $data['reg_message']='Oops! Your Data Is Unsuffient';  
                }  
           }  
           if($this->input->is_ajax_request())  
           {  
                //retrun the result when trigrred by ajax, based on validation and registration  
                echo json_encode(array('status'=>$data['status'],'message'=>$data['reg_message'],'response'=>$data['response']));  
                return;  
           }  
           else  
           {  
                // load the page seperatley when no ajax/java scripts disabled.  
           }  
      }  
 ?>  

From controller to get the form data in user_register_data function. Then here make empty variable for each fields that all are stored to a particular variable. It make to the assign to given variable so we use foreach loop here. Then that all details stored in $detail_fields. it return to the controller.

 <?php  
 public function user_register_data($register_data)  
   {  
        //empty array stored in $register_fields  
        $register_fields=array('user_name','gender','dob','ud_religion_id','ud_caste_id','std_code','ph_no','user_email','user_password');  
        //form data from argument to store the $detail_fields  
        foreach($register_fields as $reg_fields)  
        {  
             if(!empty($register_data))  
             {  
                  $detail_fields[$reg_fields]=$register_data[$reg_fields];  
        }  
        else  
        {  
                  $detail_fields[$reg_fields]='';  
        }  
      }  
      //return result array  
        $this->/*inherit_class*/->session->set_userdata('register_data_details',$detail_fields);  
        return TRUE;  
   }  
 ?>  

The all details are given to the ajax reponse. Here we will redirect or other process to do.

 <html>  
 <form enctype="multipart/form-data" method="post" id="register" name="register">  
            <input type="hidden" name="<?php echo $this->security->get_csrf_token_name(); ?>" id="csrf_token" value="<?php echo $this->security->get_csrf_hash(); ?>" />  
                     <div class="row">  
   <div class="input-field col s12">  
    <input id="user_name" type="text" name="user_name" class="validate" placeholder="Name">  
    <!--<label class="active" for="first_name2">First Name</label>-->  
   </div>  
  </div>  
  <div class="row">  
   <div class="input-field col s12 homefield">  
    <input type="date" class="datepicker" name="dob" id="dob" placeholder="Date Of Birth">  
    <!--<label class="active" for="first_name2">Date Of Birth</label>-->  
   </div>  
  </div>  
  <div class="row">  
   <div class="input-field col s12 homefield">  
    <input id="user_email" name="user_email" type="text" class="validate" placeholder="Email">  
    <!--<label class="active" for="email">Email</label>-->  
   </div>  
  </div>  
  <div class="row" style="  margin-top: 5px;">  
   <div class="input-field col s12 homefield">  
    <input id="user_password" name="user_password" type="text" class="validate" placeholder="Password">  
    <!-- <label class="active" for="password">Password</label>-->  
   </div>  
  </div>  
  <div class="center-align" style="margin-bottom: 20px;">  
  <button class="btn waves-effect waves-light margin-top-25" type="submit" name="action">Sign Up  
  </button>  
  </div>  
           </form>  
 <script>  
      //view ajax  
      $('form#register').on('submit',function(){  
       $.ajax({  
            type:'post',  
            url:'<?php echo site_url; ?>home1/add_register',  
            dataType:'json',  
            data:$('#register').serialize(),  
            success:function(html){  
                 var err='';  
                if(html.status==1){  
                     err+='<p>'+html.message+'</p>';  
                     $('#error_add').html('<div id="msg" class="alert alert-danger alert-dismissable"><i class="fa fa-ban ban_icon"></i><div class="err_div">'+err+'</div></div>');  
                     window.location.href="<?php echo site_url; ?>home1/add_all_register_data/";  
                }  
                else if(html.status==0){  
                           var s=0;  
                                $.each(html.message,function(k,v){  
                                     err+='<p>'+v+'</p>';  
                                     if(s>1){  
                                          $('#register [name="'+k+'"]').focus();  
                                          s++;  
                                          }  
                                     $('#register [name="'+k+'"]').addClass('error');  
                                });  
                                $('#error_add').html('<div id="msg" class="alert alert-danger alert-dismissable"><i class="fa fa-ban ban_icon"></i><div class="err_div">'+err+'</div></div>');  
                      }  
                      else  
                      {  
                           err+='<p>'+html.message+'</p>';  
                     $('#error_add').html('<div id="msg" class="alert alert-success alert-dismissable"><i class="fa fa-check ban_icon"></i><div class="err_div">'+err+'</div></div>');  
                     window.location.href="<?php echo site_url; ?>";  
                      }  
           }  
       });  
       return false;  
  });  
 </script>  
 </html>  

session create