CodeIgniter Forums
AJAX / Post / delete record not working - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: AJAX / Post / delete record not working (/showthread.php?tid=82173)

Pages: 1 2


AJAX / Post / delete record not working - spreaderman - 06-20-2022

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.


RE: AJAX / Post / delete record not working - seunex - 06-21-2022

Simply add / before call your ajaxUrl. You need to add slash infront of AJAX url you need to call in CI4


RE: AJAX / Post / delete record not working - spreaderman - 06-21-2022

@seunex what do you mean? The path is not the problem as stated above.


RE: AJAX / Post / delete record not working - maulahaz - 06-21-2022

Did you try :
Code:
if($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);
        }



RE: AJAX / Post / delete record not working - spreaderman - 06-21-2022

(06-21-2022, 11:34 AM)maulahaz Wrote: Did you try :
Code:
if($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);
        }

So the problem is I cannot get see any $_POST head info and also cannot see $id in headers. It isn't a logic error, I think. I tried about but as $id is blank it is always "No task to delete".


RE: AJAX / Post / delete record not working - travelite - 06-22-2022

(06-21-2022, 06:48 PM)spreaderman Wrote:
(06-21-2022, 11:34 AM)maulahaz Wrote: Did you try :
Code:
if($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);
        }

So the problem is I cannot get see any $_POST head info and also cannot see $id in headers.  It isn't a logic error, I think.  I tried about but as $id is blank it is always "No task to delete".

In your Ajax call your dataType is 'json'. However the CONTENT_TYPE may not be correct in the header. Try replacing $request->getVar('id') with $request->getJsonVar('id')


RE: AJAX / Post / delete record not working - InsiteFX - 06-22-2022

You may need to add the header shown below.

Code:
$.ajax({
    url: "your url",
    headers: {'X-Requested-With': 'XMLHttpRequest'}
});



RE: AJAX / Post / delete record not working - spreaderman - 06-22-2022

(06-22-2022, 06:36 AM)InsiteFX Wrote: You may need to add the header shown below.

Code:
$.ajax({
    url: "your url",
    headers: {'X-Requested-With': 'XMLHttpRequest'}
});

Thanks for you comment. I tried that actually but no change. Also, according to the ci4 documentation for jquery the headers don't need to be explicit set. I found the documentation here.


RE: AJAX / Post / delete record not working - kenjis - 06-22-2022

See https://www.php.net/manual/en/reserved.variables.post.php
Quote:An associative array of variables passed to the current script via the HTTP POST method when using application/x-www-form-urlencoded or multipart/form-data as the HTTP Content-Type in the request.



RE: AJAX / Post / delete record not working - Gary - 06-23-2022

Without having looked at all the detail you've pasted, I've had some similar sounding problems with my ajax crud since I've upgraded from 4.1.8 to 4.2.1... perhaps some of the problem has to do with what's mentioned here: https://forum.codeigniter.com/showthread.php?tid=82015&pid=397607#pid397607 (?)