Send data by POST from a DataTable

1

I am using a DataTable created dynamically from a DB in which I capture the event onclick of which row was selected and I collect your data, now what I need is that I redirect to another php file (I want to send them by POST) in which must be captured the data sent and depending on that data load certain information.

the code of js where I capture the data is this:

$('#Jtabla tbody').on('click', 'tr', function () {

        if ( $(this).hasClass('selected') ) {
            $(this).removeClass('selected');
            dato =table.row( this ).data();
        }
        else {
            table.$('tr.selected').removeClass('selected');
            $(this).addClass('selected');
            dato =table.row( this ).data();

            $('<form action="proyectos.php" method="POST">' + 
           '<input type="hidden" name="id" value="' + dato+ '">' +
           '</form>').submit();

             //intente con esto pero me sale este error en consola
//Form submission canceled because the form is not connected
        }
    } );

Here I would believe that they should be sent by ajax, but I know that the ajax makes the request and returns, and I need to take the data there. I remain attentive, thank you very much

    
asked by srJJ 23.07.2018 в 23:23
source

1 answer

1

The problem is that you have to connect the form first to the document before you can send it. You can do it like this:

$('#Jtabla tbody').on('click', 'tr', function () {

    if ( $(this).hasClass('selected') ) {
        $(this).removeClass('selected');
        dato =table.row( this ).data();
    }
    else {
        table.$('tr.selected').removeClass('selected');
        $(this).addClass('selected');
        dato =table.row( this ).data();

        $('<form action="proyectos.php" method="POST">' + 
       '<input type="hidden" name="id" value="' + dato+ '">' +
       '</form>').appendTo('body').submit();

    }
} );
    
answered by 24.07.2018 / 00:05
source