Capture, store and process
validated form submission with
E’s ...eForm + eForm2db
eForm2db
Version: 0.1 [Beta 1]
Author: pixelchutes
Requirements:
eForm 1.4+
Use:
- Takes validated eForm submittal data for use with the MODx DBAPI extender class.
Reference: /manager/includes/extenders/dbapi.mysql.class.inc.php
- Easily process database records referencing your form submission data:
- INSERT new records to a table of your choice
- UPDATE existing records if you’d prefer
- DELETE records based on form submittal criteria
- Form a query and return its record set as XML using $modx->db->getXML
- Query a table’s MetaData using $modx->db->getTableMetaData, followed by an update query on only the form fields that are named after table columns!
- and much more!
The eForm2db snippet:
<?php
function eForm2db( &$fields )
{
/*---------------------------------------------------------------
eForm2db
Version: 0.1 [Beta 1]
Author: pixelchutes
---------------------------------------------------------------
Requirements:
eForm 1.4+
---------------------------------------------------------------
Use:
- Takes validated eForm submittal data for use with the MODx DBAPI extender class.
- Easily process database records referencing your form submission data:
+ INSERT new records to a table of your choice
+ UPDATE existing records if you'd prefer
+ DELETE records based on form submittal criteria
+ Form a query and return its record set as XML using $modx->db->getXML
+ Query a table's MetaData using $modx->db->getTableMetaData, followed by an update query
on only the form fields that are named after table columns!
+ and much more!
---------------------------------------------------------------*/
// Bring needed resources into scope
global $modx, $table_prefix;
// Init our array
$dbTable = array(); // key = DB Column; Value = Insert/Update value
// Insert field/value pairs to insert/update in our table
$dbTable[name] = $fields[first_name].' '.$fields[last_name]; // Merge two form fields together
$dbTable[datetime] = date( 'YmdHis', strtotime( $fields[postdate] ) ); // Massage the postdate timestamp to be MySQL insert friendly
// INSERT - $dbQuery = $modx->db->insert( $dbTable, $table_prefix . 'insertTableName' );
// UPDATE - $dbQuery = $modx->db->update( $dbTable, $table_prefix . 'updateTableName' );
// DELETE - $dbQuery = $modx->db->delete( $table_prefix . 'deleteFromTableName', 'some_field = 1 AND name=\''.$dbTable[name].'\'', '' );
// etc...
// Run the db insert query
$dbQuery = $modx->db->insert( $dbTable, $table_prefix . 'insertTableName' );
return true;
}
// Return empty string
return '';
?>
Implementation:
Add the snippet call
before your
eForm snippet call:
Utilize eForm’s "callback functions" in your eForm snippet call:
[!eForm? &noemail=`true` &formid=`contact` &eFormOnBeforeMailSent=`eForm2db` &tpl=`contactform` &thankyou=`contactthanks` !]
NOTES:
- &noemail param is optional. Here, I have used this call solely as a db capture form. Since no email is sent, there is no need to define things like: to, from, report, subject, etc. Simply disable mailing, and use the callback function to process your form’s
validated fields!
- This is my first attempt at giving back to this community. MODx is definitely the next level. While I realize eForm2db is not modular in any way-shape-form, it is indeed its direction.
What do you think? Would this make a better Module? Plugin? etc... What direction should we take this to allow modular, customizable processing of eForm submittal?
More on the DBAPI:
http://www.modxcms.com/dbapi.html