Save data input from a datatable as a array

To save input form data from a DataTable, you can use the DataTable rows().data() method to get the data from the table, and then send it to the server via an AJAX request.

Here is an example code snippet that demonstrates how to save input form data from a DataTable using jQuery and AJAX:

// Get the DataTable instance
var table = $('#example').DataTable();

// Handle form submission
$('#form').submit(function(event) {
  event.preventDefault();

  // Get the data from the DataTable
  var data = table.rows().data().toArray();

  // Send the data to the server
  $.ajax({
    url: 'save.php',
    method: 'POST',
    data: { data: data },
    success: function(response) {
      alert('Data saved successfully!');
    },
    error: function(xhr, status, error) {
      console.error(error);
      alert('Error saving data!');
    }
  });
});

In this example, the #example element represents the DataTable, and the #form element represents the form that contains the submit button. When the form is submitted, the code retrieves the data from the DataTable using the rows().data() method and converts it to an array using the toArray() method. The data is then sent to the server using an AJAX POST request to the save.php script. Finally, a success or error message is displayed based on the response from the server.

In the save.php script, you can retrieve the data using the $_POST['data'] variable and process it as needed.

Komentar

Postingan populer dari blog ini

WhatsApp Web login QR code in an HTML page using whatsapp-web.js

Node.js Telegram Bot API send an image with text

Add these security headers to your website