Passing messages from server to client in Editor
Passing messages from server to client in Editor
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
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.:Allan
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.Do I need to do some more action from the server side?
Visham.
Perhaps you need:
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
Hi Allan,
You got it right! It works. From the client side I can get the customResults values"
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.