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

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

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

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);
        }
Reply
#5

(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".
Reply
#6

(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')
Reply
#7

You may need to add the header shown below.

Code:
$.ajax({
    url: "your url",
    headers: {'X-Requested-With': 'XMLHttpRequest'}
});
What did you Try? What did you Get? What did you Expect?

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

(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.
Reply
#9

See https://www.php.net/manual/en/reserved.v...s.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.
Reply
#10

(This post was last modified: 06-23-2022, 11:18 AM by Gary.)

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...#pid397607 (?)
Reply




Theme © iAndrew 2016 - Forum software by © MyBB