We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 11798
    • 43 Posts
    Hi there,

    today, I came across Bob's great tutorial on his site ( http://bobsguides.com/custom-db-tables.html ) showing how to work with custom tables and xPDO. The first part worked well, but later when I tried the section "Saving Data From a Form" I got into trouble.

    No matter what I do, the script wont run into the first If-statement, although the $_POST array is properly filled. I checked that with another snippet.
    ( Schema is already created manually during the tutorial steps.)


    Here is the code:

    'CreateQuote' Snippet
    <?php
    /* CreateQuote Snippet */
    
    
    if (isset($_POST['submit']) && $_POST['submit'] == 'send') {
    /* 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);
    $output = print_r($fields);
    
    if ( $quote->save()) {
    $output = "Quote Created";
    } else {
    $output = "Error";
    }
    
    
    } else {
    /* Not a repost, just display the form */
    /*$output = "POST-Content:" . print_r($_POST); #always empty
    $output .= $modx->getChunk('quotationTpl'); */
    $output = $modx->getChunk('quotationTpl');
    }
    return $output;
    


    'quotationTpl' Chunk
    <form action="[[~[[*id]]]]" method="post" class="form">
     
        <label for="topic">
            Topic:     
        </label>
        <input type="text" name="topic" id="topic" />
     
        <label for="quote">
            Quote:       
        </label>
        <input type="text" name="quote" id="quote" />
     
        <label for="author">
            Author:       
        </label>
        <input type="text" name="author" id="author" />
      
        <input type="submit" value="send" name="submit" />
        
    </form>
    


    I don't where I made the mistake or what I forgot to add. Prolly I forgot an essential part... Any hint is appreciated. Many thanks in advance.

    Best regards
    Hardy

    System:
    MODX Revolution 2.2.2-pl
    PHP 5.2.14
    Apache 2.2.10, Linux
    MySQL 5.0.67

    Installed Packages:
    TinyMCE
    Captcha
    CMPGenerator
    DitsNews
    FormIt

    This question has been answered by Bruno17. See the first response.

    [ed. note: Hardoman last edited this post 14 years, 4 months ago.]
    • discuss.answer
      • 4172
      • 5,888 Posts
      Can't see anything what can be wrong here.

      Did you call your snippet uncached?
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 3749
        • 24,544 Posts
        Be sure you have something like this at the top of the snippet (with the $path and table prefix set correctly):

        $result = $modx->addPackage('quotes',$path . 'model/','bobs_');
        if (! $result) {
          return 'failed to add package';
        } 




        ------------------------------------------------------------------------------------------
        PLEASE, PLEASE specify the version of MODX you are using.
        MODX info for everyone: http://bobsguides.com/modx.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
          • 11798
          • 43 Posts
          Many thanks for your replies,

          @Bruno: After I called it uncached
          [[!CreateQuote]]
          the POST-Array had been filled. I have to keep this in my mind. I bet this was a beginner's mistake when dealing with snippets. smiley



          @Bob:

          Many thanks for the hint! It will be more secure now. I updated it to this:

          /* CreateQuote Snippet */
          
          
          /* Path and check if package can be added */
          $path = MODX_CORE_PATH . 'components/quotes/';
          
          $result = $modx->addPackage('quotes',$path . 'model/','bobs_');
          if (! $result) {
            return 'failed to add package';
          }
          
          /* Checking if form has been submitted */
          if (isset($_POST['submit']) && $_POST['submit'] == 'send') {
          /* 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);
          #$output = print_r($fields);
          
          if ( $quote->save()) {
          $output = "Quote Created";
          } else {
          $output = "Error";
          }
          
          
          } else {
          /* Not a repost, just display the form */
          $output = $modx->getChunk('quotationTpl');
          }
          return $output;
          


          Many thanks to you guys. The database is now being updated properly!
          Have a nice day. [ed. note: Hardoman last edited this post 14 years, 4 months ago.]
            • 3749
            • 24,544 Posts
            Glad you got it sorted. Thanks for reporting back. smiley



            ------------------------------------------------------------------------------------------
            PLEASE, PLEASE specify the version of MODX you are using.
            MODX info for everyone: http://bobsguides.com/modx.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