CodeIgniter Forums

Full Version: when deleting record from DB the page cannot be rediirected using AJAX
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
<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>
public function deleteFile($markid)
     if ($this->User_Model->delete_file($markid))
         $status = 'success';
         $msg = 'File successfully deleted';
         $status = 'error';
         $msg = 'Something went wrong when deleting the file, please try again';
     echo json_encode(array('status' => $status, 'msg' => $msg));

public function delete_file ($markid){
       $this->db->where('certid', $markid);
       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.
PHP Code:

There you go again using capital letters when they should be lowercase.
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...

success: function(data)
    if(data.status == 'success'){
    } else {
         //show the error message somewhere
        $(someSelector).text = data.msg;

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