We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 25226
    • 29 Posts
    ------UPDATE-----http://modxcms.com/forums/index.php/topic,61460.msg351538.html#msg351538 to go to the end of the thread

    I have been searching for a Revolution 2.0 basic step by step tutorial on submitting form data to mysql db and have had no luck. I have reveiwed several othe posts and still not working. I am not looking for anything fancy, just the basics.

    Can someone please provide the most basic breakdown of a self contained functional form to db(yes I have tried FORMIT2DB)?

    -the document form html(only have to include 1 or 2 fields and submit button
    -the snippet(s) required
    -db schema and classes needed with example

    If anyone can answer this, I think there will be a ton of newbies that would be greatful.

    Thanks - Rob
      • 4172
      • 5,888 Posts
      (yes I have tried FORMIT2DB)?

      what did you try in detail/ which issues did you have?
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 3749
        • 24,544 Posts
        Are you talking about existing MODx objects (e.g., resources, chunks, users, etc.) or a custom table?

        If it’s a custom table, I’ve just added a section here about using forms to create and edit records in a custom table (the code in that section has yet to be tested):

        http://bobsguides.com/custom-db-tables.html
          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
          • 25226
          • 29 Posts
          Going through the tutorial completely with creating your example Custom Table and XpdoClasses my [[CreateQuote]] Snippet looks like this:
          <?php
          global $modx;
          if (isset($_POST['submit']) && ($_POST['submit'] == 'submit')) {
             /* Form has been submitted */
             /* You might want to check for empty required fields here */
                
            $fields = array(
                'topic' => $post['topic'],
                'quote' => $post['quote'],
                'author' => $post['author'],
             );
             $quote = $modx->newObject('Quotation', $fields);
             if ( $quote->save()) {
                 $output = "Quote Created";
             } else {
                 $output = "Error";
             }
               } else {
              /* Not a repost, just display the form */
              $output = $modx->getChunk('quotationTpl');
          }
          return $output;
          
          


          But is giving the following error
          Fatal error: Call to a member function save() on a non-object in C:\wamp\www\modx\core\cache\elements\modsnippet\38.include.cache.php on line 20

          Which is:
          if ( $quote->save()) {

          I am also calling it from a page under resources
          <form method="post" action="[[CreateQuote]]">
             
          
                          <textarea rows="5" cols="35" name="topic" id="topic"></textarea>
                          <textarea rows="5" cols="35" name="quote" id="quote"></textarea>
                          <textarea rows="5" cols="35" name="author" id="author"></textarea>
                          <input type="submit" name="submit" id="submit" value="submit"/>
          
          </form>
          


          Honestly not quite sure what to do with the [[quotationTpl]] chunk. Currently the only code thats in the chunk is [[CreateQuote]]

            • 4172
            • 5,888 Posts
            I think you didn’t load your package with $modx->addPackage(.....................);
              -------------------------------

              you can buy me a beer, if you like MIGX

              http://webcmsolutions.de/migx.html

              Thanks!
              • 25226
              • 29 Posts
              Returns same error, maybe you can clarify where to put and what to include in

              " = $modx->addPackage(xxxx)
                • 4172
                • 5,888 Posts
                This is from Bob’s Tutorial:

                $path = MODX_CORE_PATH . 'components/quotes/';
                $result = $modx->addPackage('quotes',$path . 'model/','bobs_');
                if (! $result) {
                  return 'failed to add package';
                } else {
                  $output = '<p>added Package</p>';
                }


                you need something like that in your script before you can use your objects.
                  -------------------------------

                  you can buy me a beer, if you like MIGX

                  http://webcmsolutions.de/migx.html

                  Thanks!
                  • 25226
                  • 29 Posts
                  Its definetely going in the right direction. Made the following changes and now its adding a line to the custom db table, but its NOT capturing the fields data from the form. Its just adding a blank line.
                  if (isset($_POST['submit']) && ($_POST['submit'] == 'submit')) {
                     /* Form has been submitted */
                     /* You might want to check for empty required fields here */
                       
                      $path = MODX_CORE_PATH . 'components/quotes/';
                  $result = $modx->addPackage('quotes',$path . 'model/','bobs_');
                  if (! $result) {
                    return 'failed to add package';
                  } else {
                    $quote = $modx->newObject('Quotation', $fields);
                    $fields = array(
                        'topic' => $post['topic'],
                        'quote' => $post['quote'],
                        'author' => $post['author'],
                     );
                  
                    $quote->save();
                    }
                   return $output;
                  }
                  


                  and again, this is my form

                  <form method="post" action="[[CreateQuote]]">
                     
                  
                                  <textarea rows="5" cols="35" name="topic" id="topic"></textarea>
                                  <textarea rows="5" cols="35" name="quote" id="quote"></textarea>
                                  <textarea rows="5" cols="35" name="author" id="author"></textarea>
                                  <input type="submit" name="submit" id="submit" value="submit"/>
                  
                  </form>
                  


                  Thanks again for the help
                    • 4172
                    • 5,888 Posts
                          'topic' => $post['topic'],
                          'quote' => $post['quote'],
                          'author' => $post['author'],


                    =>

                          'topic' => $_POST['topic'],
                          'quote' => $_POST['quote'],
                          'author' => $_POST['author'],
                      -------------------------------

                      you can buy me a beer, if you like MIGX

                      http://webcmsolutions.de/migx.html

                      Thanks!
                      • 25226
                      • 29 Posts
                      That did not work. It actually made to where nothing posted to db.

                      To me its seems like something here
                      if (isset($_POST['submit']) && ($_POST['submit'] == 'submit'))


                      or here doesnt look right. But I dont know
                      $quote = $modx->newObject('Quotation', $fields);