Welcome Guest, Not a member yet? Register   Sign In
AJAX / Post / delete record not working
#1

Still having some problems with AJAX crud. For delete, I have the following controller;

PHP Code:
    public function task_delete(){
        
log_message('error'var_dump($_POST));
        
$id $this->request->getVar('id');
        if(empty(
$id)){
            
$response = [
                
'success' => 0,
                
'msg' => "No Task To Delete",
            ];
            echo 
json_encode($response);
        } else {
            
$task = new TaskModel();
            
$task->task_delete($id);
            
$response = [
                
'success' => 1,
                
'msg' => "Task Deleted",
            ];
            echo 
json_encode($response);
        }
    } 

It takes in an $id from $_POST data to delete the specified task. I always get returned 'success' '= 0, msg' => "No Task To Delete". Strangely, I get NULL is my log file for var_dump($_POST)

The ajax request is as follows;

Code:
$(document).on('click', '.delete-task', function(event){
  
    let id = $(event.currentTarget).attr('data-id');
    $.ajax({        
        url:  ajaxUrl+'/admin/tasks/delete',
        type: 'post',
        // what data you passing.
        data: {
            'id' : id
        },
        processData: true,
        contentType: false,
        dataType: 'json',
        success: function(data) {
            console.log(data);
            alert("data is"+JSON.stringify(data));
        }
    });
});

The URL is correct as my log is written to.
For type, I have tried 'POST' and 'post'
For id, this gets passed in the network window, id=103 where 103 is the row I clicked on.
The alert returns success = 0 and 'success' '= 0, msg' => "No Task To Delete".

I am stumped as to what the problem is. Any pointers appreciated.
Reply


Messages In This Thread
AJAX / Post / delete record not working - by spreaderman - 06-20-2022, 10:42 PM



Theme © iAndrew 2016 - Forum software by © MyBB