recorrer un data table para obtener datos he insertar en BD

recorrer un data table para obtener datos he insertar en BD

gabcontreras42gabcontreras42 Posts: 2Questions: 1Answers: 0

hola compañeros como puedo hacer para recorrer los rows de un datatable para luego insertarlos en una table detalle, la idea es poder hacer una especie factura que cuando se ingresen los productos, estos se listen en el datatable y luego insertar esos registros en una tabla detalle en la base de datos Mysql

soy nuevo en programación actualmente estoy intentando hacerlo con php javascript y mysql, agradezco mucho alguna guía por donde comenzar muchas Gracias de Antemano.

Answers

  • colincolin Posts: 15,146Questions: 1Answers: 2,586

    You would need to send the data from the browser to a server-side script and upload into the database.

    Another option would be to use Editor. This example (from this thread) manually copies records from one table to another - with both syncing to a database. You could have the second table not in the DOM, and still use the Editor API to copy selected records (or all records) into the database,

    Colin

  • gabcontreras42gabcontreras42 Posts: 2Questions: 1Answers: 0

    hi colin thanks for you comment , but i fill the data table manual mode whith inputs, when click a button insert data in the table,now i need understand how i can take every row of my datatable an insert in Mysql,

    now i think use each(), but i dont how to use , can you help me ?

    thank you so much

  • rf1234rf1234 Posts: 2,808Questions: 85Answers: 406

    You can send the entire data table to the server. This is a button for that purpose that you can use with your data table.

    //send the entire data table to the server
    $.fn.dataTable.ext.buttons.sendTable = {
        text: "Send Table to Server",
        action: function ( e, dt, button, config ) {
            var table = dt.rows().data().toArray();
            $.ajax({
                type: "POST",
                url: 'actions.php?action=TOBEDEFINED',
                data: {
                    table: JSON.stringify(table)
                },
                success: function () {
                    // do something
                }
            });
        }
    };
    

    On the server you can do this for example:

    $table = json_decode(filter_input(INPUT_POST,'table'));
    

    That shoud give you an array of all of your table rows which you can loop through and INSERT them into your database table.

Sign In or Register to comment.