POSTing multiple rows -- issue with array I think. Thoughts appreciated.

POSTing multiple rows -- issue with array I think. Thoughts appreciated.

jim_worldwidejim_worldwide Posts: 1Questions: 1Answers: 0

Hi all! New to datatables and jquery in general, and loving it so far. I'm having an issue that I've been trying to work through for a few days and figured I'd try to seek help here.

I'm displaying a table successfully, and column 0 contains a domain name in it. I've got it to where I can select multiple rows, and what I want is to push a button that clears the rows from the table, and sends the value of column 0 (the domains) to my server for additional processing. I've almost got this figured out, but I'm stuck getting my PHP file to read more than the first domain. Here's a look at what I have so far:

HTML of button:

<button type="button" class="btn btn-block btn-info" id="whitelist">Whitelist Selected</button>

and the JS:

<script>
$(document).ready(function(){
   var domain_whitelist = "";
   var table = $('#detections').DataTable( {
      autoWidth : false,
      order : [[ 8, "asc" ]],
      lengthMenu : [[100, 200, 500, -1], [100, 200, 500, "All"]],
      ajax : "/detections.php"
   }
};
$('#detections tbody').on('click', 'tr', function() {
   $(this).toggleClass('selected');
   domain_whitelist = table.cells('.selected',0).data();
});
$('#whitelist').click( function () {
   table.rows('.selected').remove().draw( false );
   console.log(domain.whitelist);
   $.ajax({
      type : "POST",
      url : '/whitelist.php',
      data : { 'data' : domain_whitelist[0] },
      success : function (result) {
         alert(result)
      },
    });
  });
});
</script>

and then finally the PHP so far is simple until I can get the data, but it's:

<?php
   var_dump[$_POST];

<?php
>
```
?>


When I select multiple rows and push the button, I see in the console an array of all of the rows I selected.

Console.log looks like this:  

["domain1.com", "domain2.com", "domainX.com", context: Array[1], selector: Object, ajax: Object]
```

However, when I post the contents to whitelist.php, I've only been able to figure out how to read a single row value (domain_whitelist[0])... I can't figure out what to do to be able to read the entire array. If I change it to domain_whitelist without [0] I just get Object Object over on the server side.

Any thoughts? Loving this learning process. :)

Answers

  • allanallan Posts: 64,368Questions: 1Answers: 10,627 Site admin

    Hi,

    Try using:

    table.cells('.selected',0).data().toArray()
    

    note the addition of toArray() to convert the DataTables API instance to a plain array. That will make it much easier to just use a regular for or foreach loop at the server-side.

    Allan

This discussion has been closed.