06 March 2021

How to upload file in database with ajax in codeigniter?

Upload file in the database with ajax in Codeigniter?



Paste in View


<form id="doc_table" name="doc_table" method="post" enctype="multipart/form-data"   >
<?php //echo validation_errors(); ?> 
<?php //echo form_open_multipart('upload/do_upload'); ?>
      <div class="modal-body">
        
  <div class="form-group">
<label for="formGroupExampleInput">Document Name</label>
<input type="text" class="form-control doc_name" id="formGroupExampleInput" name="doc_name"  placeholder="Example input">
<label for="exampleFormControlTextarea1">Comment</label>
<textarea class="form-control doc_comment" id="exampleFormControlTextarea1" name="doc_comment"  rows="3"></textarea>
<label for="exampleFormControlFile1">Upload File</label>
<input type="text" name="lead_id" value="<?php echo $leads[0]['id']; ?>">
<input type="file" class="form-control-file doc_file" name="doc_file"  id="exampleFormControlFile1">
  </div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
        <input type="submit" value="Save" name="upload_documents" class="lead_upload_docs" class="btn btn-primary">
      </div>
  </form>


<script>



jQuery('#doc_table').submit(function(e){
e.preventDefault();
 
var doc_name = jQuery('.doc_name').val();
var doc_comment = jQuery('.doc_comment').val();
var doc_file = jQuery('.doc_file').val();
var leadid =  <?php echo $leads[0]['id']; ?>;


if(leadid){
jQuery.ajax({
url:'<?php echo base_url(); ?>leads/add_lead_docs',
type:"post",
data:new FormData(this),
processData:false,
contentType:false,
cache:false,
async:false,
success:function(response){  
console.log(response);
}
});

}else{
jQuery('#result_notice').html('');
}
  
});

</script>




Paste in Controller




  public function add_lead_docs(){
  


$lead_id = $this->input->post('lead_id');
$doc_name = $this->input->post('doc_name');
$doc_comment = $this->input->post('doc_comment');
$config['upload_path']          = 'assets/upload';
            $config['allowed_types']        = 'gif|jpg|png';
$config['encrypt_name'] = TRUE;
                $this->load->library('upload', $config);
                if ( ! $this->upload->do_upload('doc_file'))
                {
                        $error = array('error' => $this->upload->display_errors());
print_r($error);
echo 'FIle Error';
                }
                else
                {

                        $data = array('upload_data' => $this->upload->data());
echo 'FIle Uploaded Successfully';
echo $file_name = $this->upload->data('file_name');
                } 
$data = array(
                    'doc_name' => $doc_name,
                    'doc_comment' => $doc_comment,
                    'doc_path' => $file_name,
'p_id' => $lead_id,
                    'type' => 'doc'
); 
    $this->Leads_model ->add_documents($data); 
 
  
  }




Paste in Model

function add_documents($data)
    {
$insert = $this->db->insert('leads_history', $data);
$insert_id=$this->db->insert_id();
return $insert_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_...