CodeIgniter Forums
when deleting record from DB the page cannot be rediirected using AJAX - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: General Help (https://forum.codeigniter.com/forum-24.html)
+--- Thread: when deleting record from DB the page cannot be rediirected using AJAX (/thread-71875.html)



when deleting record from DB the page cannot be rediirected using AJAX - kvanaraj - 10-04-2018

Code:
View
******
<td align="center">
<a href="<?php echo base_url().'Users/deleteFile/'.$row['markid']; ?>" onclick="return confirm('Are you sure to delete data?')"  name="idv"  <i class="fa fa-remove" style="font-size:25px;color:red"></i>
</td>
Code:
Controller
**********
public function deleteFile($markid)
 {
     if ($this->User_Model->delete_file($markid))
     {
         $status = 'success';
         $msg = 'File successfully deleted';
         redirect('Users/upload');
         
     }
     else
     {
         $status = 'error';
         $msg = 'Something went wrong when deleting the file, please try again';
     }
     echo json_encode(array('status' => $status, 'msg' => $msg));
     //redirect('Users/upload','refresh');
   
 }

Code:
Model
*******
public function delete_file ($markid){
       $this->db->where('certid', $markid);
       $this->db->delete('gallery');
       if ($this->db->affected_rows() == '1')
   {
     return TRUE;
   }
   
   return FALSE;        
   }
the records are successfuly deleted, but the page cannot redirect it goes to dashboard. any alternate option suggestion please.


RE: when deleting record from DB the page cannot be rediirected using AJAX - InsiteFX - 10-04-2018

PHP Code:
redirect('users/upload','refresh'); 

There you go again using capital letters when they should be lowercase.


RE: when deleting record from DB the page cannot be rediirected using AJAX - dave friend - 10-04-2018

An AJAX response sends all output to the AJAX success callback.

What you need to do is use the "status" property as returned to success to decide what action to take - in your case a redirect via JavaScript.

Assuming you're using JQuery, the JS looks roughly like this...

Code:
success: function(data)
{
    if(data.status == 'success'){
        window.location.replace("http://example.com/users/upload");
    } else {
         //show the error message somewhere
        $(someSelector).text = data.msg;
    }
}

In case it's not clear, remove any redirect calls from the controller method