This topic is locked

Show Changed Fields in an email

8/27/2007 9:15:12 AM
PHPRunner Tips and Tricks
G
gshafer author

[codebox]global $conn,$strTableName;
// Enter the user name into the LastEditBy field

$values["LastEditBy"] = $_SESSION["UserID"];
// Compare old and new values. If value has changed add user ID and Updated Field Text

foreach ($values as $key => $value)

{

if ($value!=$oldvalues[$key])

$message .= $key . ": " . $value . " Edited by:".$_SESSION["UserID"]." Updated Field \n";

else

$message .= $key . ": " . $value . "\n";

}
$email="test@test.com";

$subject="Field Update";

mail($email, $subject, $message);
return true;[/codebox]This code is used in the BeforeEdit Event.
It inserts the username of the person doing the edit in the field "LastEditBy".
It also sends an email showing all of the fileds but highlighting the changed field and who changed it.
....George

M
mmponline 8/30/2007

How can I change this code to send it to the User (Logged in User) as well.

M
mmponline 9/14/2007

Also
How do I prevent an e-mail to be sent if no changes are made. In other words if auser saves the field without making changes.

G
gshafer author 10/3/2007

Also

How do I prevent an e-mail to be sent if no changes are made. In other words if auser saves the field without making changes.


Just set a variable to 0 at the beginning of your program and then set it to 1 if you find a field that has changed. Before sending the email check to see if the variable is set before sending the email.
$sendemail = 0;
if ($value!=$oldvalues[$key])

{

$message .= $key . ": " . $value . " Edited by:".$_SESSION["UserID"]." Updated Field \n";

$sendmail = 1;

}
...
if ($sendmail)

mail($email, $subject, $message);

G
giles 10/16/2007

Good post George. <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=22275&image=1&table=forumreplies' class='bbc_emoticon' alt=':D' /> Been trying to figure out something like this for a while...
Stephan,

For your first question on how to send an email to the logged in user I use the following:

  1. User has an email field in the "users" table
  2. An After successful login event sets a session variable to the user's email address...
    global $conn;

    $rs = db_query("select * from users where username='".$_SESSION["UserID"]."'", $conn);

    $data = db_fetch_array($rs);
    $_SESSION["userEmail"] = $data["email"];
  3. In George's code add the user's email via the session variable...
    $email="test@test.com".", ".$_SESSION["userEmail"];