Recording Form into a Database Table

With JsonFormBuilder writing data from a form into a database table can really be done very simply by using xPDO commands. Of course to write to an xPDO object you need to first create one. There are many guides around but Bob Ray has a great example here http://bobsguides.com/custom-db-tables.html.

Once you have an instance of your xPDO created, loaded and ready to go, you can simply add data using a little custom validation after submission.

For this example, lets assume we want to record out basic contact form into a test table. Our package will be called "testpackage" and the table itself will be called "testpackage_contactlog". Lets auto create the xPDO structure using Bob Rays CreateXpdoClasses snippet like so.

[[!CreateXpdoClasses? &myPackage=`testpackage` &myPrefix=`testpackage_`]]

Under core/components/testpackage/ you will now see the xPDO structure generated (if everything has been done correctly). For this example I created a table to record name_full (varchar), email_address (varchar), comments (text) and we'll also record a copy of the email in email_html_content and the submitted time within the time_submitted column. The generated testpackage.mysql.schema.xml file is shown below.

<?xml version="1.0" encoding="UTF-8"?>
<model package="testpackage" baseClass="xPDOObject" platform="mysql" defaultEngine="MyISAM" version="1.1">
	<object class="Contactlog" table="contactlog" extends="xPDOSimpleObject">
		<field key="name_full" dbtype="varchar" precision="128" phptype="string" null="true" />
		<field key="email_address" dbtype="varchar" precision="128" phptype="string" null="true" />
		<field key="comments" dbtype="text" phptype="string" null="true" />
		<field key="email_html_content" dbtype="text" phptype="string" null="true" />
		<field key="time_submitted" dbtype="datetime" phptype="datetime" null="true" />
	</object>
</model>

All you now need to do is load your package and store the data after the form is valid and submitted. Right after adding your form elements (but before calling the output method), add your database entry code like so.

A full script is available within core/components/jsonformbuilder/docs/examples/JsonFormBuilder-record-in-db.php

//After creating all your form elements add them
$o_form->addElements(
    array(
        $o_fe_name,$o_fe_email,$o_fe_notes,$o_fe_buttSubmit
    )
);

//Load our custom package. This could be done well before even running your form snippet.
//As long as the package is loaded prior to using your custom database you will be fine.
$packageName = 'testpackage';
$path = MODX_CORE_PATH . 'components/'.$packageName.'/';
$packageLoadResult = $modx->addPackage($packageName,$path.'model/',$packageName.'_');
if($packageLoadResult===false){
    //log or throw an error in some way.
   echo 'Failed to load package "'.$packageName.'"';
   exit();
}

//must force form to run validate check prior to checking if submitted
$o_form->validate();
    
if($o_form->isSubmitted()===true && count($o_form->getInvalidElements())===0){
    //form was submitted and is valid. Now we can record the data to the databse table
    //Of course you may want to secure some of the post variables before they enter your database as well.
    $o = $modx->newObject('Contactlog');
    $o->set('name_full',$o_form->postVal('name_full'));
    $o->set('email_address',$o_form->postVal('email_address'));
    $o->set('comments',$o_form->postVal('comments'));
    $o->set('email_html_content',$o_form->getEmailContent());
    $o->set('time_submitted',time());
    $o->save();
}

//The form HTML will now be available via the output call
//This can be returned in a snippet or passed to any other script to handle in any way.
$o_form->output();