Welcome Guest, Not a member yet? Register   Sign In
when deleting record from DB the page cannot be rediirected using AJAX
#1

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.
Reply
#2

(This post was last modified: 10-04-2018, 09:48 AM by InsiteFX.)

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

There you go again using capital letters when they should be lowercase.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

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
Reply




Theme © iAndrew 2016 - Forum software by © MyBB