Submit selected in PHP

Submit selected in PHP

khmelev77khmelev77 Posts: 6Questions: 3Answers: 0
edited March 2019 in Free community support

I have such a table:

<div class="container">
    <form action="" method="POST">
    <table id="example" class="display" style="width:100%">
        <thead>
            <tr>
                <th>Name</th>
                <th>Position</th>
                <th>Office</th>
                <th>Age</th>
                <th>Start date</th>
                <th>Salary</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>Tiger Nixon</td>
                <td>System Architect</td>
                <td>Edinburgh</td>
                <td>61</td>
                <td>2011/04/25</td>
                <td>$320,800</td>
            </tr>
            <tr>
                <td>Garrett Winters</td>
                <td>Accountant</td>
                <td>Tokyo</td>
                <td>63</td>
                <td>2011/07/25</td>
                <td>$170,750</td>
            </tr>
        </tbody>
        <tfoot>
            <tr>
                <th>Name</th>
                <th>Position</th>
                <th>Office</th>
                <th>Age</th>
                <th>Start date</th>
                <th>Salary</th>
            </tr>
        </tfoot>
    </table>
        <button name="example" type=submit>Send POST to PHP</button>
    </form>
</div>

<script>
$(document).ready(function() {
var table = $('#example').DataTable( {
    dom: 'Bfrtip',
    select: true,
    buttons: [
        {
            text: 'Select all',
            action: function () {
                table.rows().select();
            }
        },
        {
            text: 'Select none',
            action: function () {
                table.rows().deselect();
            }
        }
    ]
} );
} );
</script>

I need to make sure that when I click on "Send POST to PHP", the selected data is sent to a php script on the server. Example:
<?php print("Полученный POST:") print_r($_POST); ?>

Answers

  • colincolin Posts: 15,237Questions: 1Answers: 2,598

    Hi @khmelev77 ,

    To get the selected rows, just use rows(), so something like table.rows({select:true}).data(), then send it off in an ajax call in a click event for that button.

    Cheers,

    Colin

  • khmelev77khmelev77 Posts: 6Questions: 3Answers: 0
    edited March 2019

    Hello, I can not understand where to insert it. And I can not find an example in the documentation. My failed attempt:
    $(document).ready(function() {
    var table = $('#example').DataTable( {
    dom: 'Bfrtip',
    select: true,
    buttons: [
    {
    text: 'Select all',
    action: function () {
    table.rows().select();
    }
    },
    {
    text: 'Select none',
    action: function () {
    table.rows().deselect();
    }
    },
    {
    text: 'Send POST',
    action: function () {
    table.rows({select:true}).data();
    }
    }
    ]
    } );
    } );

  • allanallan Posts: 63,075Questions: 1Answers: 10,384 Site admin

    So here:

    action: function () {
      table.rows({select:true}).data();
    }
    

    You have an action function that is getting the selected row data (although it should be selected: true). But nothing is done with the data. Use $.ajax or similar to submit the data to the server.

    Allan

This discussion has been closed.