How to inject column data into Email notification using 'postCreate' function?
How to inject column data into Email notification using 'postCreate' function?
Dear Allan, the ->on( 'postCreate', function()) is working well, thanks for it. It would be great to inject column data into it. Perhaps, using $values might bear the solution to it, but I could not figure it out, how to get it done. Please, advice! Many thanks, Miklos
Answers
When you say column data are you referring to the data of the row that was just created? If yes then use the second parameter,
json
, of thepostCreate
event to access the returned row data.If the new row contains other generated data that you need then use the fourth parameter,
id
, to access the the row data usingrow().data()
with theselector
as the string #id. I built a simple example that you can add a new row to and see the structure of thejson
response and usingrow().data()
to get the new row.https://live.datatables.net/guwafemu/623/edit
Is this what you are looking for? If not please provide more details of what column data you want to use.
Kevin
Dear Kevin, thanks for your ind and quick reply. Yes, that is what I am looking for! I will go ahead and check what you have built and try to implement it. Many thanks in advance, will report back.
Miklos
OK, now I see you have probably thought I would like to log changes. Indeed, what I would like is to send email notification with newly inserted data.
To clarify this for you I place my code below.
Many thanks!
Right, sorry I misunderstood. I don't use the Editor server side libraries but I believe you will want to refer to this postCreate event doc. Looks like the first parameter is
data
which is the data read from the database. I suspect that is what you want to use. I might be wrong but I think you have the incorrect parameters listed for the events in your above code.Kevin
Also:
Isn't valid PHP. That looks like the client-side
postEdit
function, not the server-side one by the same name (which I can see how it is confusing!).Your
postCreate
andpostRemove
look fine, but check the manual forpostEdit
.The final parameter (
$row
) passed to the event handler is what you should use to create your email.Allan
Many thanks to both of you for your kind and valuable help!
I have followed your instruction, Allan, but it does not work unfortunately.
->on( 'postEdit', function ( $editor, $id, $values, $row ) {
mail( 'myself@icloud.com, herself@icloud.com, herself@laparo.hu', 'Elojegyzes modositva!', 'Előjegyzés módosítva: '.$id.' sorszámú előjegyzésnél módosítás történt.' );
} )
->on( 'postCreate', function ( $editor, $id, $values, $row ) {
mail( 'myself@icloud.com, herself@icloud.com, herself@laparo.hu', 'Uj elojegyzes!', 'Új előjegyzés '.$row.' sorszámmal rögzítve.' );
} )
->on( 'postRemove', function ( $editor, $id, $values ) {
mail( 'myself@icloud.com, herself@icloud.com, herself@laparo.hu', 'Elojegyzes torolve!', 'Törölve: a(z) '.$id.' sorszámú előjegyzés törölve.' );
} )
Without knowing the details of what is not working and any error messages you might be getting. My guess is that you need to access the specific properties (fields) of the
$row
object. I don't use PHP so the syntax might not be correct but maybe something like$row->name
.Kevin
Dear Kevin, I appreciate you efforts! I got it working partially with the code below:
->on( 'postEdit', function ( $editor, $id, $values, $row ) {
mail( '@icloud.com, @icloud.com, @laparo.hu', 'Elojegyzese modositva!', 'Előjegyzés módosítva: '.$row['name'].' előjegyzésénél módosítás történt.' );
} )
Now data from column "name" is injected into the body of the email. However, I would still need more data from additional columns to be inserted, that I have not been able to achieve so far. Any idea would be highly appreciated.
Best regards,
Miklos
Looks like the PHP email() method expects a string for the third
message
parameter. You will need to build a string but concatenating the different fields with the text you want. For example:Kevin
Dear Kevin,
I am grateful to you for your continuous support! It helped me a lot to get to the current state of my code - almost perfect.
The only tiny thing I have not been able to solve so far is the missing whitespace between data injected into the body of the email message. I have thoroughly checked the available descriptions about PHP syntax to solve it - no luck so far. The problem is minor, it can stay as is, but if you have any idea it would be awesome.
Once more, many-many thanks for all your suggestions and support.
Yours sincerely,
Miklos
- below you may kindly find my code
->on( 'postCreate', function ( $editor, $id, $values, $row ) {
mail( '@icloud.com, @icloud.com, @laparo.hu', 'DV GYN Team - Elfogadta, bejegyezve!', $row['op'].$row['date'].', operatőr: '.$row['surgeon'], 'Új műtét előjegyezve: '.$row['name'] );
} )
Are you asking about adding new lines? If yes then see if this SO thread helps.
Kevin
One thing, if you are sending email from PHP, I'd very much suggest using something like PHPMailer, rather than the built in
mail()
if you are sending to external addresses and deliverability is important.Allan