This topic is locked
[SOLVED]

 Send email if checkbox is checked

3/17/2021 7:26:46 AM
PHPRunner General questions
Tandy author

Okay, Now I have most of my trip sheet done. I am having one last problem maybe someone could help out. Only in the edit if they mark the checkbox to complete I need that to shoot off an email. here is what I got in EVENT - Edit - Before record updated:


//********** Check if Trip is LOCKED ************

$rs = DB::Query("SELECT * FROM trip_sheet WHERE locked = '1' ");

$data=$rs->fetchAssoc();

if($data)

{

//********** Start Send email when Trip Marked Complete ************

$email= "MY EMAIL";

$from= "From Email";

$fromName= "FROM WHOME";

$msg= "Here is a trip that has been marked COMPLETE\r\n";

$subject= "Trip Marked Complete";
$msg.= "Name: ".$values["driver_name"]."\r\n";

$msg.= "Truck Number: ".$values["truck_number"]."\r\n";

$msg.= "Trip Number: ".$values["trip_number"]."\r\n";

$msg.= "Link to Trip:MYDOMAIN/trip_sheet_view.php?editid1=".$values["trip_id"]."&\r\n";
$ret=runner_mail(array('to' => $email, 'subject' => $subject, 'body' => $msg, 'fromName'=> $fromName, 'from' => $from));

if(!$ret["mailed"])

echo $ret["message"];

//********** END Send email when Trip Marked Complete ************

}

else

{

// if dont exist do something else

}
// Place event code here.

// Use "Add Action" button to add code snippets.
return true;


The email sends off fine. The problem is it will send off if the checkbox is checked or not so the bad part in this code I think is around:

$rs = DB::Query("SELECT * FROM trip_sheet WHERE locked = '1' ");

$data=$rs->fetchAssoc();

Please let me know if I am on the right path..
Thanks

James

K
keithh0427 3/17/2021

Looks to me that it will trigger an email if ANY record in the trip_sheet table has locked set to '1'

admin 3/17/2021

I see two issues here.

  1. You use BeforeEdit event which means the record is not saved yet
  2. You run a select that would pick the first record from the database where locked is set to 1. As long as you have at least one such a record in the database the email will be sent.
    Move your code to AfterEdit event. Instead of using this select SQL query simply inspect the value of 'locked' field like this:

if ($values["locked"]==1) {

// send the email

}
Tandy author 3/17/2021



I see two issues here.

  1. You use BeforeEdit event which means the record is not saved yet
  2. You run a select that would pick the first record from the database where locked is set to 1. As long as you have at least one such a record in the database the email will be sent.
    Move your code to AfterEdit event. Instead of using this select SQL query simply inspect the value of 'locked' field like this:

if ($values["locked"]==1) {

// send the email

}




Wow that was easy.. That worked like a charm.. Thought there would be a lot more to it..
Thank You very much