Welcome Guest, Not a member yet? Register   Sign In
codeigniter ajax database error handling
#1
Photo 
(This post was last modified: 06-01-2021, 10:08 AM by jackey.)

When I try to add duplicate entry, it give error like this.
I want to the error "Duplicate entry '[email protected]' for key 'email' " to be visible in my view page inside div tag as alert-danger
Can any one pls help me 

Code:
///////////View////////
<form id="form_temp_student" name="form_temp_student" action="#" >
            <table class="table align-middle table-bordered table-striped">                                     
                        <tbody>

                            <tr>
                                <td>Full Name </td>
                                <td colspan="3">  <div class="form-control"><input type="text" name="full_name" id="full_name" style="width:900px" required><span class="help-block"></span></div> </td>
                               
                            </tr>
                            <tr>
                                <td> Initials</td>
                                <td colspan="3"> <div class="form-control"><input type="text" name="initials" id="initials" style="width:200px" required><span class="help-block"></span></div> </td>
                           
                            </tr>
                            <tr>
                                <td> First Name</td>
                                <td> <div class="form-group"><input type="text" name="first_name" id="first_name" style="width:200px" required> <span class="help-block"></span></div></td>
                           
                                <td>Last Name </td>
                                <td> <div class="form-group"><input type="text" name="last_name" id="last_name" style="width:300px" required> <span class="help-block"></span> </div></td>
                           
                            </tr>

<tr>
                                <td> email</td>
                                <td colspan="3"> <input type="email" name="email2" id="email2" style="width:300px" required> </td>
                            </tr>

</tbody>
            </table>
          </form>
          <button type="button" class="btn btn-default" id="btn_add_emp" onClick="add_temp_student()"> &nbsp;SUBMIT</button>
          <button type="button" class="btn btn-default" id="btn_add_cancel"> &nbsp;CANCEL</button>
 


PHP Code:
function add_temp_student()
    {      
 
  
var formData = new FormData($('#form_temp_student')[0]);
    
   
          
$.ajax({
            url "<?php echo site_url('index.php/Login/add_temp_student')?>",      
            type
"POST",
          dataformData,
          contentTypefalse,
          processDatafalse,      
            dataType
"JSON",
            
            success
: function(data)
            {
                if(data.status
                        

                            location.reload();

                        }
                        else
                        {
                            $("#message").html(data.message); 
                        }
            },
            error: function (jqXHRtextStatuserrorThrown)
            {
                $("#message1").html(data.message);

         
            
}
        });    
    


//////////////controller//////////


PHP Code:
  public function add_temp_student() {           
    $this
->load->library('form_validation');
    $this->form_validation->set_rules('full_name','Full Name','trim|required');
    $this->form_validation->set_rules('initials','Initials','trim|required');
    $this->form_validation->set_rules('last_name','Last_name','trim|required');
    $this->form_validation->set_rules('email2','email','trim|required');
    $this->form_validation->set_rules('first_name','First Name','trim|required');              
    
    
if ($this->form_validation->run() == false) {
            $response = array(
                'status' => FALSE,
                'message' => '<div class="alert alert-danger alert-dismissible"  role="alert"> <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true"></span></button>'.validation_errors().'</div>'
            );

            echo json_encode($response);
    }
    else {
                          $data = array( 
                          'full_name' => $this->input->post('full_name'),
                          'initials' => $this->input->post('initials'),
                          'first_name' => $this->input->post('first_name'),  
                          
'last_name' => $this->input->post('last_name'),
                          'email'=> $this->input->post('email2')
                        );

        $this->load->model('Login_model');
        $result=$this->Login_model->model_add_temp_student($data);

                  if($result){ 
                          $this->session->set_flashdata('message_ok''Your Data successfully added to the system. You will be contacted via email');
                          echo json_encode(array("status" => TRUE));
                  }else {
               
                
                      $response2 
= array(
                      'status' => FALSE,
                      'message' => '<div class="alert alert-danger alert-dismissible"  role="alert"> <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true"></span></button>' .$this->db->error().'</div>'
                  );
                  echo json_encode($response2);
                 


                  
}

    }
 

////////////model//////////
PHP Code:
public function model_add_temp_student($data){
  $succes_insert=$this->db->insert('student'$data);

  if ($succes_insert) {
            return true;
        } else {
       
            $this
->session->set_flashdata('message_error','<div class="alert alert-danger">  Please try again !</div>'.$this->db->error()); 
         
        
}



Attached Files Thumbnail(s)
   
Reply


Messages In This Thread
codeigniter ajax database error handling - by jackey - 06-01-2021, 10:01 AM



Theme © iAndrew 2016 - Forum software by © MyBB