1

Introduction: I am doing a server-side datatables library using PHP and Ajax. When a user clicks the edit button I want to transfer that id to PHP so I can later update it...

I am basically doing these two stack-overflow articles combined:

Index.php

<script type="text/javascript"> 
$(document).on('click','.edit_btn',function (){
var edit_id = $(this).attr("id");
alert(edit_id);
    $.ajax({
         url: 'index.php',
      data: { edit_id : edit_id },
        contentType: 'application/json; charset=utf-8',
        success: function(result) {
              alert(result);
        }
    });
});
</script>

Edit button code:

columnDefs:
[
    {  
        targets: -1,
        render: function (data, type, row, meta) {
            return '<input type="button" class="delete_btn" id=n-"' + meta.row + '" value="delete"/> <input type="button" class="edit_btn" id=s-"' + meta.row + '" value="edit" name="edit_btn"> </a>';
        }
    }
]

I believe datatables library does not recommend using the get method from the URL. What am I missing? Thanks.

  • `edit_row_id` needs to be `edit_id ` and it needs to be `var edit_id = $(this).attr("id");`that's it. rest 2 lines not needed. direct do `alert` and check after that – Alive to die - Anant Sep 21 '18 at 10:29
  • How does a row look look like? `var edit_row_id = $(this).attr("id").match(/\d+/)[0]; var edit_id = $('#example').DataTable().row( edit_row_id ).data(); var edit_id = edit_id[0];` seems to be a lot just for retrieving an `id` – davidkonrad Sep 21 '18 at 10:30
  • In the script here you lack a `});` at the end + how do you create your `edit_btn`? I work with datatable too and when I need a button I display my button like this : ``, this way I have the id very easily when I click on it – Mickaël Leger Sep 21 '18 at 10:33
  • columnDefs: [ { targets: -1, render: function (data, type, row, meta) { return ' '; } } ] –  Sep 21 '18 at 10:38
  • that is my edit button i will edit it in. –  Sep 21 '18 at 10:38
  • yeah i noticed the }); at the end before you said something but getting an error when i add one in too –  Sep 21 '18 at 10:40
  • ok. so i updated all your suggestions and it alerts a number now, just not the right one (it is alerting the number in the array). so if it is the first button it says 0. but great step in right direction! thanks. new code is edited as well. –  Sep 21 '18 at 10:43
  • @ScottSchmidt on both button code add `data-id` attribute and put corresponding record `id`(incremented `id` in table) as a value in them. now do :- `var edit_id = $(this).data("id"); alert(edit_id);` this will give you exact record `id` what you want to edit – Alive to die - Anant Sep 21 '18 at 10:47
  • var id = $(this).attr("id").match(/\d+/)[0]; var del_id = $('#example').DataTable().row( id ).data(); worked for delete but not edit. –  Sep 21 '18 at 10:53
  • Figured it out. I can post final code as solution. You were 99% on the right track. –  Sep 21 '18 at 10:58

1 Answers1

0

After reading the comments, I got this final solution to work:

 <script type="text/javascript"> 
    $(document).on('click','.edit_btn',function (){
          var id = $(this).attr("id").match(/\d+/)[0];
            var edit_id = $('#example').DataTable().row( id ).data();
            var edit_id = edit_id[0];
    alert(edit_id);
        $.ajax({
             url: 'index.php',
          data: { edit_id : edit_id },
            contentType: 'application/json; charset=utf-8',
            success: function(result) {
                  //alert(result);
            }
        });
    });
    </script>

Thank you to all who helped!