We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 18367
    • 834 Posts
    I’m making slow headway with eform2db.

    At present, new entries are being added to the db, but the actual data isn’t being written. EG I get a new entry and id # in the db but the actual fields for "name 1" and "name 2" remain empty.

    Can anyone enlighten me?

      Content Creator and Copywriter
      • 7231
      • 4,205 Posts
      Show us your code grin
        [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

        Something is happening here, but you don't know what it is.
        Do you, Mr. Jones? - [bob dylan]
        • 18367
        • 834 Posts
        Somehow I knew someone would ask for that..

        Call

        <p>[[eForm2db]]
        [!eForm? &noemail=`true` &formid=`eformtest` &eFormOnBeforeMailSent=`eForm2db` &tpl=`eformtest` &thankyou=`372` !]</p>


        Chunk

        [+validationmessage+]
        <form method="post" action="[~[*id*]~]" id="eformtest">
            <fieldset>
                <h3>Hatcheries Directory</h3>
                <label for="cfName">Company Name:
                <p><input name="Name" id="cfName" type="text" value="[+name+]" eform="Name::1:"/></p></label>
         
                <label for="cfName2">Name2:
                <p><input name="Name2" id="cfName2" type="text" value="[+name2+]" eform="Name2::1" /></p></label>
         
                
                <p><input type="submit" class="button" value="Submit" /></p>
            </fieldset>
        </form>


        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['name'] = $fields['name'];
                $dbTable['name2'] = $fields['name2'];
               // $dbTable[datetime] = date( 'YmdHis', strtotime( $fields[postdate] ) ); // Massage the postdate timestamp to be MySQL insert friendly
        
        
                // INSERT - $dbQuery = $modx->db->insert( $dbTable, $table_prefix . 'dbtest' );
                // 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 . 'dbtest' ); 
        	       
        
                return true;
            }
           // Return empty string
            return '';
        ?>


        I had a hard time just getting this far.

        Thanks in advance

          Content Creator and Copywriter
          • 25483
          • 741 Posts
          I think that this part is wrong:

          // Insert field/value pairs to insert/update in our table
                  //$dbTable[name] = $fields[first_name].' '.$fields[last_name]; // Merge two form fields together
                  $dbTable['name'] = $fields['name'];
                  $dbTable['name2'] = $fields['name2'];
                 // $dbTable[datetime] = date( 'YmdHis', strtotime( $fields[postdate] ) ); // Massage the postdate timestamp to be MySQL insert friendly
          


          Try this:
          // Insert field/value pairs to insert/update in our table
                  //$dbTable[name] = $fields[first_name].' '.$fields[last_name]; // Merge two form fields together
                  $dbTable[name] = $fields[name];
                  $dbTable[name2] = $fields[name2];
                 // $dbTable[datetime] = date( 'YmdHis', strtotime( $fields[postdate] ) ); // Massage the postdate timestamp to be MySQL insert friendly
          


          So remove the quotes ( ’ )
            with regards,

            Ronald Lokers
            'Front-end developer' @ h2o Media

            • 18367
            • 834 Posts
            Ronald,

            made no difference.

            An entry but no data.
              Content Creator and Copywriter
              • 25483
              • 741 Posts
              hmz... ok...

              But wait... I see that you have Capitals in your "names" in your html form. Maybe that’s why it is not working, so try to use the exact names with the capitals.
                with regards,

                Ronald Lokers
                'Front-end developer' @ h2o Media

                • 18367
                • 834 Posts
                Hallelujah,

                That worked.

                Here’s what I did, I basically changed every instance of Name to name.

                [+validationmessage+]
                <form method="post" action="[~[*id*]~]" id="eformtest">
                    <fieldset>
                        <h3>Hatcheries Directory</h3>
                        <label for="cfname">Company Name:
                        <p><input name="name" id="cfname" type="text" value="[+name+]" eform="name::1:"/></p></label>
                 
                        <label for="cfname2">name2:
                        <p><input name="name2" id="cfname2" type="text" value="[+name2+]" eform="name2::1" /></p></label>
                 
                        
                        <p><input type="submit" class="button" value="Submit" /></p>
                    </fieldset>
                </form>




                (I still have the quotes in the snippet, though. I read in another thread that it was best practice to use the quotes, but hell I wouldn’t know.))

                Many thanks I’ve been struggling with this all week. smiley
                  Content Creator and Copywriter