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

(06-23-2022, 11:14 AM)Gary Wrote: 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 (?)

Appreciate the comment. Actually, I have not updated yet. Don't see anything that would impact me.
Reply
#12

(06-23-2022, 08:00 PM)spreaderman Wrote:
(06-23-2022, 11:14 AM)Gary Wrote: 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 (?)

Appreciate the comment.  Actually, I have not updated yet.  Don't see anything that would impact me.

@kenjis I noticed you provided a link for associative arrays with form use. Why? I thought I am sending a javascript object data: {'id' : id}. Also, I am not using a form. I am get the record id but clicking on the respective row with let id = $(event.currentTarget).attr('data-id'); In the payload/network tab in chrome, I log console.log(data); and I see id=103. Is that what I should expect?
Reply
#13

(This post was last modified: 06-25-2022, 12:59 AM by spreaderman.)

It seems I was posting a JSON object.  Almost there but not.

Here is the revised javascript:

Code:
$(document).on('click', '.delete-task', function(event){
    // id comes from data-id
    let id = $(event.currentTarget).attr('data-id');
    $.ajax({
        url:  ajaxUrl+'/admin/tasks/delete',
        type: 'post',
        data: {
            "id" : id
        },
        headers: {
            "Content-Type": "application/json",
            "X-Requested-With": "XMLHttpRequest"
        },
        processData: true,
        contentType: false,
        dataType: 'json', // expected dataType back from the server
        success: function(data) {
            console.log(data);
            alert("hi: "+JSON.stringify(data));
        }
    });
});

I can get the raw JSON stream with the new code but cannot figure out how to access id;


PHP Code:
public function task_delete(){
 
  log_message('error'print_r($this->request->getRawInput(), TRUE));
 
  
   $data 
$this->request->getJsonVar('id');
 
  log_message('error'print_r($datatrue));
 
  
     $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);
 
    }
 } 

The $this->request->getRawInput() produces;

Code:
ERROR - 2022-06-25 07:43:52 --> Array
(
    [id] => 100
)

But how to access id pls?

According to the documentation, if the CONTENT-TYPE is set to application/json, I should be able to access like this;  $request->getVar('id'); but it is blank.

Code:
            [Content-Type] => CodeIgniter\HTTP\Header Object
                (
                    [name:protected] => Content-Type
                    [value:protected] => application/json
                )
Reply
#14

If you're run out of other ideas,  you could try setting processData: false (?)... it seems like you're doing the serializing manually.  If nothing else, it may give you a few clues...

Also looking at the network transfers' headers & responses in the client browser should point you to the source of problem and a fix pretty quickly (?).
Reply
#15

@Gary thanks for your comment.

I have rewritten the jquery ajax per below and I can now getVar!!! The only problem now is getting the 'id' to my model.

Here is my jquery ajax:

$(document).on('click', '.delete-task', function(event){
let id = $(event.currentTarget).attr('data-id');
data = {};
name = 'id';
value = id;
data[name] = value;
$.ajax({
url: ajaxUrl+'/admin/tasks/delete',
type: 'post',
data: data,
success: function(data) {
console.log(data);
alert("hi"+JSON.stringify(data));
}
});
});

In my old script, I was not formatting the 'date' correctly. It was previously showing up in the payload tab (chrome devtools) as id=103. With the above script, the payload now show 'id: 103'.

All working now. Than you all for you comments and suggestions.
Reply
#16

Pass the id into your model method when you call it.

PHP Code:
// Model
public function yourMethod(int $id 0)
{
    if ($id 0)
    {
        // Error
    }
    else
    {
        // Ok
    }
}

// Controller call Model
$model->yourMethod($id); 

or you can getVar in the mode.
What did you Try? What did you Get? What did you Expect?

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

@spreaderman First you said you can't get $_POST. So I pointed the link for explanation of $_POST.
$_POST is only for POST request and HTML Form.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB