0

Good Morning,

I am working with datatables and this example

Here my test:

        
        var t = $('#example').DataTable( {
            rowReorder: true
        } );
        
        $('button').click( function() {
            var data = t.$('input').serialize();
            console.log(data);
            return false;
        } );
        
<html>
<head>
    <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/dt/dt-1.11.3/r-2.2.9/rr-1.2.8/datatables.min.css"/>
</head>
<body>


<button type="submit">Submit form</button>
<table id="example" class="display" style="width:100%">
    <thead>
        <tr>
            <th>ID</th>
            <th>Fname</th>
            <th>Lname</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>1</td>
            <td><input type="text" name="fname" value="Max" /></td>
            <td><input type="text" name="lname" value="Mustermann" /></td>
        </tr>
        <tr>
            <td>2</td>
            <td><input type="text" name="fname" value="Max" /></td>
            <td><input type="text" name="lname" value="Mustermann" /></td>
        </tr>
    </tfoot>
</table>    


<script type="text/javascript" src="https://cdn.datatables.net/v/dt/jq-3.6.0/dt-1.11.3/r-2.2.9/rr-1.2.8/datatables.min.js"></script>

    
</body>
</html>

If I click the button, I get this output:

fname=Max&lname=Mustermann&fname=Max&lname=Mustermann&fname=Max&lname=Mustermann

But I would like to get a array like this (jQuery or php solution):

Array
(
    [0] => Array
        (
            [fname] => Max
            [lname] => Mustermann
        )

    [1] => Array
        (
            [fname] => Max
            [lname] => Mustermann
        )

)

I have no idea how I can realize it. Can you help me please?? :)

** UPDATE **

Result (Rory)

Array
(
    [example_length] => 10
    [fname] => Array
        (
            [0] => Max
            [1] => Max
        )

    [lname] => Array
        (
            [0] => Mustermann
            [1] => Mustermann
        )

)
Trombone0904
  • 4,132
  • 8
  • 51
  • 104
  • this will give another structure. please look at my first post – Trombone0904 Oct 20 '21 at 09:07
  • You're right - apologies. In that case the only way to build the data structure you require from the HTML would be to use JS then send the array via AJAX to your PHP backend. – Rory McCrossan Oct 20 '21 at 09:09
  • The following may help: [How to send a JSON object using html form data](https://stackoverflow.com/questions/22195065/how-to-send-a-json-object-using-html-form-data) and [Convert form data to JavaScript object with jQuery](https://stackoverflow.com/questions/1184624/convert-form-data-to-javascript-object-with-jquery). – andrewJames Oct 20 '21 at 12:42

0 Answers0