0

I have an editable HTML table, and I'd like to submit the entire table to a PHP page as a single csv variable. The PHP page will then parse the csv and do its thing in the background. I don't want to use a single input per table cell because some of my tables are large and I will quickly run into truncation from POSTing more than 1000 variables. But a csv of the table is not too big and easy for me to parse on the backend.

So I'm looking for a package or method that will take a table like this

<table id="thetable">
    <tr>
        <td>cellA</td>
        <td>cellB</td>
    </tr>
    <tr>
        <td>cellC</td>
        <td>cellD</td>
    </tr>
</table>

and submit it through POST as a variable, where the resulting variable on the server side would be something like

cellA,cellB\n
cellC,cellD
Greg B
  • 609
  • 6
  • 19
  • Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it. – Jay Blanchard May 18 '16 at 19:33

2 Answers2

0

Take a look at that fiddle.

The implementation is basically these map functions on the cols and rows. You can use that exportTableToCSV function to get the desired string and just remove the download part.

inafalcao
  • 1,415
  • 1
  • 11
  • 25
0

My first thought is to look through the <tr> of the table, then inside that loop, loop through the <td> values. If you are just grabbing the value inside you can use the .html()

I haven't tested this, so it may need to be altered slightly.

So let's say you give your table an id of data-table

var data = [];

$('#data-table tr').each(function() {
  $('td', $(this)).each(function() {
     data.push($(this).html());
  });
});

Then join your array with a comma separator.

Jhorra
  • 6,233
  • 21
  • 69
  • 123