Send email on row edit with editor
Send email on row edit with editor

Im trying to get an email to fire off when an edit is made.
include( "lib/DataTables.php" );
use
DataTables\Editor,
DataTables\Editor\Field,
DataTables\Editor\Format,
DataTables\Editor\Mjoin,
DataTables\Editor\Options,
DataTables\Editor\Upload,
DataTables\Editor\Validate,
DataTables\Editor\ValidateOptions;
Editor::inst( $db, 'rma_submissions', 'ID' )
->fields(
Field::inst( 'rma_submissions.id' ),
Field::inst( 'rma_submissions.timestamp' )
->validator( Validate::dateFormat( 'Y-m-d H:i:s' ) )
->getFormatter( Format::datetime( 'Y-m-d H:i:s', 'Y-m-d H:i:s' ) )
->setFormatter( Format::datetime( 'Y-m-d H:i:s', 'Y-m-d H:i:s' ) ),
Field::inst( 'rma_submissions.location' ),
Field::inst( 'rma_submissions.orig_invoice' ),
Field::inst( 'rma_submissions.all_accessories' ),
Field::inst( 'rma_submissions.new_submission' ),
Field::inst( 'rma_submissions.man_sku' ),
Field::inst( 'rma_submissions.device_id' ),
Field::inst( 'rma_submissions.failure_desc' ),
Field::inst( 'rma_submissions.defective_oob' ),
Field::inst( 'rma_submissions.original_sales_date' ),
Field::inst( 'rma_submissions.vendor' ),
Field::inst( 'rma_submissions.email' ),
Field::inst( 'rma_submissions.device_man' ),
Field::inst( 'rma_submissions.district' ),
Field::inst( 'rma_submissions.rma_number' ),
Field::inst( 'rma_submissions.paid' ),
Field::inst( 'rma_submissions.paid_date' ),
Field::inst( 'rma_reasons.reason_description' ),
)
->on( 'postEdit', function ( $editor, $id, $values, $row ) {
})
->leftJoin( 'rma_reasons', 'rma_reasons.reason_code', '=', 'rma_submissions.failure_desc' )
->on( 'postEdit', function ( $editor, $id, $values, $row ) {
$to = "sku@mysite.net";
$subject = "rmaUpdate Completed";
$message = "
<html>
<head>
<title>rmaUpdate Complete</title>
</head>
<body>
<p>This is a copy of your rma Update Request</p>
<table>
<tr><td>Email: <b> $row['email']</b></td></tr>
<tr><td>Notes: <b> $row['failure_desc']</b></td></tr>
<tr><td>RQ Sku: <b> $row['man_sku']</b></td></tr>
<tr><td>Completed?: <b> Yes </b></td></tr>
</table>
</body>
</html>
";
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers
$headers .= 'From: <support@mysite.net>' . "\r\n";
$headers .= 'cc: <'.$row['email'].'>' . "\r\n";
mail($to,$subject,$message,$headers);
} )
->process( $_POST )
->json();
The above saves the data but gives me an error with my editor.
Any suggestions?
This question has accepted answers - jump to:
This discussion has been closed.
Answers
That error message means that the server has responded with invalid JSON. Hopefully there is an error message in there. What is that error message? If you click the “More information” link it will show you how to get that information.
Allan
Its returning the correct data it appears.
I did update my script a bit.
I did remove the 1st instance of the ->on postedit in this. It doesnt allow me to edit my post.
also, if i remove the
$values2
from the script it works fine. It must be trying to parse data that is not there. How would I get the information from the table and row that was edited?@allan
How can I include
js editor.on( 'submitError', function ( e, xhr, err, thrown, data ) { this.error( xhr.responseText ); } );
in this example? Ive tried a few different ways and it fails to load.Ok so I kinda have this working but want to know why I have to store this in a session for it to work.
Shouldnt I be able to just use
$row[rma_submissions']['id']
?If i use the above without session it sends the email and saves to DB but still shows me an error. So its working but throwing an error somewhere. How and where can i diagnose this? Console and networking show no errors and 200 status.
Yes.
As I mentioned, the error you are seeing means that invalid JSON has been returned by the server. It might well contain some valid JSON, but there is something in there that it making the message as a whole invalid. So the key is to use the browser’s network inspector and go to the “Response” tab (not “Preview”) for the request and see what is there.
Allan
@allan Thank you for the response. So this is the response that I got.
That’s valid JSON, which would not result in the “system error” message.
Can you give me a link to the page so I can check it out please? You can send me a PM by clicking my name above and then “Send message”, if you don’t want to make the url public.
Allan
@allan I got it working and it is now sending emails. I am not sure what was the issue but it is now working. One more issue though. If i set a where clause it does not send email.
See code below
Here is the response
It wouldn't
. Your condition on the
where
statement is:And in your
postEdit
:It can't ever be
1
if your where condition is restricting it to 0.Allan