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

    Im trying to populate a form based on a lookup of a database, I can easily write to a database using the formit2db snippet but what I want to do is retrive data from the database based on a sql query from a previous form.

    E.g a visitor lands on a page, and fills in their email address and presses submit, this then passes them onto a new page with a different form. this form looks up the database based on their email address and prefills the other forms.

    I cant use formit retreiver as it only retains data from the other subitted form and im no good at writing prehooks so any ideas?

    Thanks
      • 3749
      • 24,544 Posts
      I think one way would be a custom snippet that read the data and populated hidden fields in the first form. Then FormIt Retriever would have access to them.
        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
      • But would you be able to retrieve the data to fill the hidden fields before the user has submitted his email?
          Studying MODX in the desert - http://sottwell.com
          Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
          Join the Slack Community - http://modx.org
          • 30912
          • 463 Posts
          Thats the problem im having. I need to do the lookup AFTER the submit to populate the next form, as that data would be unique to each email address (name company etc)
          • There's no other way to do it, you need a pre-hook to fetch the data for the fields. A pre-hook is just a snippet, nothing to get excited about.

            http://rtfm.modx.com/display/ADDON/FormIt.Hooks#FormIt.Hooks-UsingpreHooks
            http://rtfm.modx.com/display/ADDON/FormIt.FormItRetriever

            Use the placeholder from FormItRetriever to specify the WHERE clause when fetching the data.
              Studying MODX in the desert - http://sottwell.com
              Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
              Join the Slack Community - http://modx.org
              • 30912
              • 463 Posts
              (if you can write snippets that is)

              time to swat up and hit the MODx book.
              • Is the email in your custom table, or is it using the user ID to relate to the user tables?
                  Studying MODX in the desert - http://sottwell.com
                  Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                  Join the Slack Community - http://modx.org
                  • 30912
                  • 463 Posts
                  its in the custom table, so far using http://forums.modx.com/index.php?topic=28633.0

                  ive managed to create:

                  $sql = "SELECT * FROM `oracle_vault`";
                  
                  $query = $modx->query($sql);
                  
                  $rows = array();
                  
                  if ($query) {
                      // loop through the result set and inspect one row at a time
                      while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
                  
                          array_push($rows, $row);
                  
                      }
                  }
                  
                  echo '<br /><br /><pre>';
                          print_r($rows);
                  echo '</pre>';


                  which outputs the sample data into the array, so the query is definately working now i just need to figure out how to set the place holders for the rows data to use with FormitRetriever and how to assign the where clause to it.
                  • Something like this:
                    $sql = "SELECT * FROM `oracle_vault` WHERE email = '".$email."'";
                    $stmt= $modx->query($sql);
                    if ($stmt) {
                        $row = $stmt->fetch(PDO_FETCH_ASSOC);
                        $modx->setPlaceholders(array(
                           'field1' => $row['field1],
                           'field2' => $row['field2']
                        ),'my.');
                    }
                    
                      Studying MODX in the desert - http://sottwell.com
                      Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                      Join the Slack Community - http://modx.org
                      • 30912
                      • 463 Posts
                      i think so, im guessing that the syntax is.

                      select all from the table oracle vault where the email matches the email submitted (.$email.) then set the corresponding placeholders of field1 and field2 (which are in the database)

                      I would then need to set someinputs on the receiving page using

                      <input type="hidden" value="[[+my.field1]]">


                      then FormItRetriever can access the fields?

                      and im guessing that it should be

                      $sql = "SELECT * FROM `oracle_vault` WHERE email = '".$email."'";
                      $stmt= $modx->query($sql);
                      if ($stmt) {
                          $row = $stmt->fetch(PDO_FETCH_ASSOC);
                          $modx->setPlaceholders(array(
                             'field1' => $row['field1'],
                             'field2' => $row['field2']
                          ),'my.');
                      }