Passing messages from server to client in Editor

Passing messages from server to client in Editor

vishamvisham Posts: 3Questions: 1Answers: 0
edited October 2023 in Free community support

Hello,
I am using datatables editor Events example (see below) to run several functions on postEdit. All functions are working fine.
Now I only need to pass a message 'Function x completed successfully' after the completion of each function to the client side.

Server Side

function logChange ( $db, $action, $id, &$values ) {
    $db->insert( 'staff-log', array(
        'user'   => $_SESSION['username'],
        'action' => $action,
        'values' => json_encode( $values ),
        'row'    => $id,
        'when'   => date('c')
    ) );
echo "Function Success message";
}
Editor::inst( $db, 'staff' )
    ->fields(
        Field::inst( 'first_name' ),
        Field::inst( 'last_name' )
 
    )
     ->on( 'postEdit', function ( $editor, $id, &$values, &$row ) {
        logChange( $editor->db(), 'edit', $id, $values );
    } )
    ->process( $_POST )
    ->json();

I tried the code above but I am getting "a system error occurred" https://datatables.net/manual/tech-notes/12 due to malformed JSON.
I tried to use json_encode but still the same issue.

echo json_encode( [ "data" => ["Function Success message"] ] );

Can anyone guide me on how to do this? I'm sure I've missed something!
Thanks
Visham.

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 63,494Questions: 1Answers: 10,470 Site admin
    Answer ✓

    Hi Visham,

    What you need to do is use ->data() rather than ->json() to get the data object that Editor constructs in ->process() and then manipulate that with the stored result. i.e.:

    <?php
    
    function logChange ( $db, $action, $id, &$values ) {
        $db->insert( 'staff-log', array(
            'user'   => $_SESSION['username'],
            'action' => $action,
            'values' => json_encode( $values ),
            'row'    => $id,
            'when'   => date('c')
        ) );
    
        return "Function Success message";
    }
    
    $results = [];
    $editor = Editor::inst( $db, 'staff' )
        ->fields(
            Field::inst( 'first_name' ),
            Field::inst( 'last_name' )
        )
        ->on( 'postEdit', function ( $editor, $id, &$values, &$row ) use ($results) {
            $results[] = logChange( $editor->db(), 'edit', $id, $values );
        } )
        ->process( $_POST );
    
    $data = $editor->data();
    $data['customResults'] = $results;
    
    echo json_encode($data);
    // customResults would contain an array if `postEdit` is executed
    

    Allan

  • vishamvisham Posts: 3Questions: 1Answers: 0

    Hi Allan,

    Thanks for your guidance on the use the data method. I modified my code as you described but, I am getting a well formed JSON but, customsResults is empty. See below.

    {
        "data": [
            {
                "DT_RowId": "row_9",
                "parent_table": {
                    "first_name": "user1",
                    "last_name": "user1a"
                },
                "child_table": []
            }
        ],
        "customResults": []
    }
    

    Do I need to do some more action from the server side?

    Visham.

  • allanallan Posts: 63,494Questions: 1Answers: 10,470 Site admin
    Answer ✓

    Perhaps you need:

     use (&$results)
    

    apologies - it has been a while since I've done that in PHP.

    If that doesn't do it, can you show me your full code as it currently stands?

    Allan

  • vishamvisham Posts: 3Questions: 1Answers: 0
    edited October 2023

    Hi Allan,
    You got it right! It works. From the client side I can get the customResults values"

    editor.on( 'postEdit', function ( e, json, data ) {
     $("#messages").html("Response: " + json.customResults[0] );
    } );
    

    I have 3 functions to execute on postEdit. Now just need to loop through the customResults[] and play with the html to display all messages.

    I did not see examples like this on the forum, hope can be useful for others to expand on it.

    Thanks a lot for your help.
    Visham.

Sign In or Register to comment.