Chad
Chad

Reputation: 2455

Datatables submit form serverside data

For those of you that use the Datatables js plugin, how can I create this example with server side data?

The example uses data that is hardcoded in the HTML.

Upvotes: 2

Views: 5803

Answers (2)

OlliM
OlliM

Reputation: 7113

I had the same problem and didn't want to do an ajax save, so I did this:

var table = $("#mytable").datatable();

$("#myform").submit(function () {
    var hiddenArea = $("<div></div").hide().appendTo("#myform");
    table.$('input:hidden').detach().appendTo(hiddenArea);

    // Prevent original submit and resubmit, so the newly added controls are
    // taken into account
    this.submit();
    return false;
});

The idea is that I take all the inputs that are currently not in the dom and move them inside a hidden container.

Upvotes: 3

Jakub
Jakub

Reputation: 20475

You would basically do the following:

  • Serialize the form data (using jquery serialize as the example shows)
  • Submit said data to your form handling scrip (php etc)

They already provide the jquery serialize code so I won't show that, however the jQuery AJAX function will be needed (at the least):

$.ajax({
   type: "POST",
   url: "some.php",
   data: YOUR-SERIALIZED-DATA-HERE,
   success: function(msg){
     alert( "Data Saved: " + msg );
   }
 });

And on your Server side PHP file you just grab the correct form array and parse your values ($_POST).

Upvotes: 3

Related Questions