Email Notification in Form
4 posts by 2 authors in: Forums > CMS Builder
Last Post: March 4, 2014 (RSS)
By design9 - February 27, 2014
Hello,
I am using the email smtp setup that is available now in the CMS settings. I have included the code in one of my forms to send an email when a form is submitted and I have set up a special email template. I am just having two issues. Here is my code:
// send message
$placeholders = array(
'user.email' => $_REQUEST['email'],
'user.fullname' => $_REQUEST['name'],
'content' => $_REQUEST['content'],
'contestAdminUrl' => $GLOBALS['SETTINGS']['adminUrl'] . "?menu=photocontests_new",
);
// email admin
$errors = sendMessage(wsc_emailTemplate_loadFromDB(array(
'template_id' => 'ADMIN-ENTRY-ADDED',
'placeholders' => $placeholders,
)));
if ($errors) { die("Error sending message: $errors\n\nPlease let us know about this error!"); }
}
}
1). In this url: 'contestAdminUrl' => $GLOBALS['SETTINGS']['adminUrl'] . "?menu=calendar&action=edit&num=" . mysql_insert_id(),
How do I pull in the record num so it goes directly to the correct entry to edit?
2). When I tested the form, the email gets sent to me with no issue. The issue is that the fields that I put in the the email template, #user.email# and #user.name# and #content# are not pulling in any of the data into the sent email. I have included a screen shot of my email template and email that was sent. I have also included my form page with code.
Thanks,
April
Hi April,
There are a couple of small errors that are stopping this form working correctly.
The reason that your place holders are empty is that on line 114 of form.php you have this line:
$_REQUEST = array();
This is unsetting all of the items in your request array, then on line 121 when you're creating your place holders after the request array has been removed. If you remove the line $_REQUEST = array(); your placeholders should send.
The second issue is that the mysql_insert_id() contains the num value that was inserted into photocontests_new, but the link in your placeholders is going to the section calendar. I think you just need to change the link to go to photocontests_new on line 125, and the link should work.
$GLOBALS['SETTINGS']['adminUrl'] . "?menu=photocontests_new&action=edit&num=" . mysql_insert_id(),
Let me know if you have any questions.
Cheers!
Greg
PHP Programmer - interactivetools.com
By design9 - February 28, 2014
Greg,
Thank you for your help. It is all working except for the url.
I had actually copied and pasted the table name and copied the wrong one (calendar). I am using the correct url now but it returns a 0 instead of the actual id/record number.
Here is what comes up now:
http://www.charlotteparent.com/cmsadmin/admin.php?menu=photocontests_new&action=edit&num=0
Is there anyway to fix that?
Thanks!
April
By gregThomas - March 4, 2014
Hi April,
It might be that as you've already called mysql_insert_id(), the last inserted value might not be stored in in any more when you call it a second time. You could try changing your code to this between lines 105 and 125:
mysql_query($query) or die("MySQL Error:<br/>\n". htmlspecialchars(mysql_error()) . "\n");
$recordNum = mysql_insert_id();
// adopt temp uploads
adoptUploads($tableName, $preSaveTempId, $recordNum);
removeExpiredUploads(); // erase old expired uploads
// display thanks message and clear form
$errorsAndAlerts = "Thanks, we've added your record!";
$_REQUEST = array();
$preSaveTempId = uniqid('x');
// send message
$placeholders = array(
'user.email' => $_REQUEST['email'],
'user.fullname' => $_REQUEST['name'],
'content' => $_REQUEST['content'],
'contestAdminUrl' => $GLOBALS['SETTINGS']['adminUrl'] . "?menu=$tableName&action=edit&num=" . $recordNum,
So I've removed the line that resets the $recordNum value to null, and I'm using that value on the end of the contestAdminUrl value.
Another way around this issue would be to insert the data using CMS Builders mysql_insert function:
if (!@$errorsAndAlerts) {
$insertArray = array(
'createdDate' => date('Y-m-d H:i:s'),
'createdByUserNum' => $CURRENT_USER['num'],
'updatedDate' => date('Y-m-d H:i:s'),
'updatedByUserNum' => $CURRENT_USER['num'] ),
'date' => date('Y-m-d H:i:s'),
'hidden' =>'1',
'name' => $_REQUEST['name'],
'content' => $_REQUEST['content'],
'email' => $_REQUEST['email'],
'category' => $_REQUEST['category'] ,
'title' => $_REQUEST['title'],
);
$recordNum = mysql_insert($tableName, $insertArray, true);
// adopt temp uploads
adoptUploads($tableName, $preSaveTempId, $recordNum);
removeExpiredUploads(); // erase old expired uploads
// display thanks message and clear form
$errorsAndAlerts = "Thanks, we've added your record!";
$preSaveTempId = uniqid('x');
// send message
$placeholders = array(
'user.email' => $_REQUEST['email'],
'user.fullname' => $_REQUEST['name'],
'content' => $_REQUEST['content'],
'contestAdminUrl' => $GLOBALS['SETTINGS']['adminUrl'] . "?menu=$tableName&action=edit&num=" . $recordNum,
);
The good thing about this function is it automatically parses the data to make it safe for MySQL, and if you pass in true as the third value, it will automatically insert a default value into any fields with missing data.
Let me know if you have any questions.
Thanks!
Greg
PHP Programmer - interactivetools.com