Doesn't SAVE data when the AJAX uses a filter variable of $ _POST

Doesn't SAVE data when the AJAX uses a filter variable of $ _POST

jpavanjpavan Posts: 38Questions: 12Answers: 0
edited October 2019 in Editor

Hi Allan, Here we are again to ask you about an ajax with -> where it includes a $ _Post parameter.
We have two tables: apm_pct (tabla_cab) and apm_pcc (tabla_det), both editable.
When a record is selected in apm_pct, that ID is retrieved to filter the results of apm_pcc.

So, our **javascript ** of apm_pcc
says:

"ajax": {
                "url": "/apm/public/../module/Abm/src/Abm/Controller/DataTables/php/abm/apm_pcc_det.php?conexion=[here conexion data]",
                "type": "POST",
                "data": function ( d ) {
                    var selected = tabla_cab.row( { selected: true } );                      
                    if ( selected.any() ) {
                        var rowIdx = tabla_cab.row( {selected: true } ).index();
                        d.codigo = tabla_cab.cell( rowIdx, 0 ).data(); 
                        alert(d.codigo);
                    } 
                }
The **server-script** of the detail table (table_det) says:
$codigo =  (isset($_POST["codigo"])) ? $_POST["codigo"] : null;
if ( ! isset($codigo) || ! is_numeric($codigo) ) { 
    echo json_encode( [ "data" => [] ] );  
} else {  
    Editor::inst( $db, 'apm_pcc', 'apm_pcc.pcc_codpcc')
->fields(
         Field::inst( 'apm_pcc.pcc_codpcc')
                                            ->validator( 'Validate::notEmpty' , array(
                                                    'message' => 'Este campo es requerido'
                                                ) )
   .... rest of the fields
->where( 'apm_pcc.pcc_codpct', $codigo )
->process( $_POST )
->json();
}

Everything works great, except that the detail table (apm_pcc or table_det) does not save the changes, it is not possible to edit and preserve the changes.
If I remove the filter from the server-script that retrieves the value of the $ _POST ['codigo'] variable, the datatable-editor works excellent and saves the changes that are made.
What is wrong in this example?
Thanks Allan,

Edited by Colin - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

Answers

  • David@QuantumDavid@Quantum Posts: 36Questions: 4Answers: 2
    edited October 2019

    Hello @jpavan ,

    I suspect it's because your anonymous function in the "data" attribute of your ajax never returns the value so adding return d.codig; after alert(d.codigo); may be the solution to your problem? If this is the case it's probably because when the POST happens the data is being passed as null (To the best of my knowledge - I may be wrong).

    Quick note for future, for readability use script based syntax highlighting, e.g. JavaScript:
    (Markdown)
    ```js
    $(document).ready( function () {
    var table = $('#myTable').DataTable();
    // ...
    } );
    ```
    (Rendered)

    $(document).ready( function () {
      var table = $('#myTable').DataTable();
      // ...
    } );
    

    For your PHP scripts you use ```php...```. For more info on this go to the Markdown Notes page.

    Hope this helps,
    David

  • jpavanjpavan Posts: 38Questions: 12Answers: 0

    Thank you David for your comments and tips!

    Adding the "return" did not achieve good results.

    However, I found the solution by detecting the instance in which the "edit" of new data occurs, and applying in the php in this way:

    <?php
    $conexion = $_GET['conexion'];
    include( '../lib/DataTables.php' );

    use DataTables\Editor;

    $codigo = (isset($_POST["codigo"])) ? $_POST["codigo"] : null;
    $action = (isset($_POST["action"])) ? $_POST["action"] : null;
    if ( $action != "edit" && ( ! isset($codigo) || ! is_numeric($codigo)) ) {
    echo json_encode( [ "data" => [] ] );
    } else {
    Editor::inst( $db, 'apm_pcc', 'apm_pcc.pcc_codpcc')
    ->fields( ......

    Thank you until next time :smile:

This discussion has been closed.