We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 349
    • 76 Posts
    How do i save data entered into a Formit form into a database?
    How do i rewrite the email-recipient based on certain conditions from the form?
      • 21872
      • 95 Posts
      *bump*

      I, too, would appreciate some guidance in accomplishing the OP’s first request. There was a really good tutorial on how to do this with eForm on the old wiki but I can’t seem to find anything similar for formit.
        • 349
        • 76 Posts
        So here is what i got yet, but now i´m stuck and don´t know how to save the data.

        I added a hook snippet "formit2db"
        [[!FormIt?
        &hooks=`spam,formit2db,email`
        &emailTpl=`myEmailChunk`
        &emailTo=`[email protected]`
        ]]

        <?php
        $errorMsg = '<pre>'.print_r($scriptProperties['fields'],true).'</pre>';
        $scriptProperties['hook']->errors['user'] = $errorMsg;
        
        $dsn= 'mysql:host='.$modx->db->config['host'].';dbname=regenata';
        $xpdo= new xPDO($dsn, $modx->db->config['user'], $modx->db->config['pass']);
        
        /**
        * Adding our package
        */
        $xpdo->addPackage('mypackage','/path/to/mypackage/model/','mp_');
        return true;
        ?>
        at the part with addPackage i don´t know what to do or if it is the right way.
        I think now i have to create a XML-File for the database model?
        I am not so experienced with MVC programming:(

        So has anyone a solution for me?
          • 3749
          • 24,544 Posts
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 36926
            • 701 Posts
            aceman3000, how did you have any luck in doing this?

            Anyone else manage to post to a database.

            Thanks

            Ben
              • 27519
              • 275 Posts
              I have the same requirement.

              I checked out the link mentioned above but it only covers part of the solution going through the modx db layer.

              For my situation I think I will write a snippet that is triggered by Formit as a custom hook.

              This snippet will gather all fields entered into the form and then kick off a PHP class that actually writes them out to database, using plain SQL and a plain mysql connection.

              For phase 1 of my project this approach will do. For a next release I will find out the nitty gritty stuff of modx db layer.
                MODx Revolution / MAMP / OS X
                • 15051
                • 7 Posts
                Quote from: skndn60 at Sep 10, 2010, 03:38 PM

                I have the same requirement.

                I checked out the link mentioned above but it only covers part of the solution going through the modx db layer.

                If you follow the link mentioned to create your model class(es) and mappings then the rest is pretty easy.

                Assuming your form field names match up with your model/table column names, saving to the database can be as easy as:

                <?php
                /**
                 * @package contactform
                 */
                $base_path = !empty($base_path) ? $base_path : $modx->getOption('core_path').'components/contactform/';
                
                // Let modx know about our package that defined the model we want to use
                $modx->addPackage('contactform',$base_path.'model/');
                
                // 1. Make a new xPDO object representing a row in the table
                $contact_form = $modx->newObject('contactForm');
                
                // 2. Populate the object using the form values (this assumes the names match up. If they don't, you'll have to
                // pull out the fields individually from the fields array and set them.
                
                // Remember, formit will call this snippet with scriptProperties
                $contact_form->fromArray($scriptProperties['fields']);
                
                // 3. Save it!
                $contact_form->save();
                
                return true;
                ?>
                
                  • 1778
                  • 659 Posts
                  Hello

                  From

                  FormIt Roadmap
                  FormIt 2.0.0
                  Save form results to DB
                  Iterative snippet to grab saved form results and display in table/graph format

                  It will happens... but for now you’ll have to write your custom hook/snippets and table schema...
                  I think I have seen somewhere in the forum a thread about a formit2db but don’t remember where...
                  Cheers
                    • 17422
                    • 225 Posts
                      • 1778
                      • 659 Posts
                      @lolotlm yes it was this one, thanks for pointing the link smiley