How do I send data from Parent Id to server-side script then use where condition?

How do I send data from Parent Id to server-side script then use where condition?

jojomejojome Posts: 3Questions: 1Answers: 0
var rowData = row.data();

var table = $('<table id="table" class="display" width="100%"/>');

row.child(table).show();

var usersEditor = new $.fn.dataTable.Editor({
    ajax: {
    url: "php/table.rpt2.php",
    type:"POST",
    data: function(d) {
            d.pinno = rowData.id;
        }
    },
    table: table,
    fields: [
        {
            label: "OWNERNAME",
            name: "ownername"
        },
        {
            label: "TDNO",
            name: "tdno"
        },
        {
            label: "PINNO",
            name: "pinno",
            def: rowData.id
        }
    ]
}); ```

This is the code that I have been using. I tried using ->where('pinno',$_POST) at the server-side script but so far I can't make it work.

This question has an accepted answers - jump to answer

Answers

  • rf1234rf1234 Posts: 2,806Questions: 85Answers: 406
    edited December 2019 Answer ✓

    $_POST is wrong. It should be $_POST["pinno"].

    If you want me to take a look post your server side code please.

    Also: I can't see what row.data() really is. Is it the data of one single row?

    Here is an example with a parent and a a child table that works:

    var selected;
    selected = parentTable.row({selected: true});
    if (selected.any()) {
        parentId = selected.data().ctr.id;
    }
    
    var editor = new $.fn.dataTable.Editor( {
        ajax: {
            url: 'actions.php?action=tblCtrCategoryWithValueRange',
            data: function ( d ) {
                d.parent_id = parentId;
            }
        },
        table: "#table",
        fields: [
                    ..............
            }
        ]
    } );
    

    Server side:

    if ( ! isset($_POST['parent_id']) || ! is_numeric($_POST['parent_id']) ) {
        echo json_encode( [ "data" => [] ] );
    } else {    
        Editor::inst( $db, 'ctr_has_ctr_category' ) 
        ->field(
            Field::inst( 'ctr_has_ctr_category.ctr_id' )->set( false ),
            ............
        )
        } )
       
        ->where( function ( $q ) {        
            $q  ->where( 'ctr_has_ctr_category.ctr_id', $_POST['parent_id'] );
        } )   
        ->process($_POST)   
        ->json();
    
  • jojomejojome Posts: 3Questions: 1Answers: 0

    @rf1234 thank you for your response. Yes row.data is the data of a single row. I verified that the

    var rowData = row.data() 
    

    has the correct value using the

    {
                    label: "PINNO",
                    name: "pinno",
                    def: rowData.pinno
                }
    

    as it will give the value of whatever row it belongs to whenever I create new entries.

    I am trying to send that value from ajax to server-side script using

    ajax: {
            url: "php/table.rpt2.php",
            type:"POST",
            data: function(d) {
                    d.pinno = rowData.pinno;
                }
            },
    

    Here is my Server-Side Script using the code you have given

    if ( ! isset($_POST['pinno']) || ! is_numeric($_POST['pinno']) ) {
        echo json_encode( [ "data" => [] ] );
        } else {   
        Editor::inst( $db, 'rpt')
        ->field(
            Field::inst( 'propertyid' )->set( false ),
            Field::inst( 'ownername' ),
            Field::inst( 'tdno' ),
            Field::inst( 'pinno' )  
        )
        } )
        
        ->where( function ( $q ) {       
            $q  ->where( 'pinno', $_POST['pinno'] );
        } )  
        ->process($_POST)  
        ->json();
    

    I received a warning "table id = table - Invalid JSON response". I did try another approach as I remove the if()else() statements then use a sample data '1'

     ->where( function ( $q ) {       
            $q  ->where( 'pinno','1' );
        } ) 
    

    to check if the code would give me a result.

  • jojomejojome Posts: 3Questions: 1Answers: 0

    thanks for the help @rf1234 ! Anyway I already solved the problem , with the example you have given I was able to understand the general gist on how to pass the value to server-side. Aside from that, I changed my script to this...

    Editor::inst( $db, 'rpt','propertyid' )
        ->field(
            Field::inst( 'propertyid' )->set(false),
            Field::inst( 'ownername' ),
            Field::inst( 'tdno' ),
            Field::inst( 'pinno' )
        )
        
        ->where( function ( $q ) {       
            $q  ->where( 'pinno', $_POST['pinno'] );
        } )  
        ->process($_POST)  
        ->json();
    

    I removed the if statement completely and it works fine now!

This discussion has been closed.