![]() |
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
|
RE: AJAX / Post / delete record not working - spreaderman - 06-23-2022 (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.php?tid=82015&pid=397607#pid397607 (?) Appreciate the comment. Actually, I have not updated yet. Don't see anything that would impact me. RE: AJAX / Post / delete record not working - spreaderman - 06-24-2022 (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.php?tid=82015&pid=397607#pid397607 (?) @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? RE: AJAX / Post / delete record not working - spreaderman - 06-24-2022 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){ 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(){ The $this->request->getRawInput() produces; Code: ERROR - 2022-06-25 07:43:52 --> Array 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 RE: AJAX / Post / delete record not working - Gary - 06-25-2022 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 (?). RE: AJAX / Post / delete record not working - spreaderman - 06-25-2022 @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. RE: AJAX / Post / delete record not working - InsiteFX - 06-26-2022 Pass the id into your model method when you call it. PHP Code: // Model or you can getVar in the mode. RE: AJAX / Post / delete record not working - kenjis - 06-26-2022 @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. |