We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 33372
    • 1,611 Posts
    Quote from: sottwell at Dec 03, 2006, 03:11 AM

    I was thinking that the $fields array needs to be populated, but eForm has to be faked into believing that it’s reloading the form after a failed validation, only without the error messages.

    I was thinking that, too. But I haven’t been able to get it to work. It seems as if I should be able to just set $_POST[’formid’] and eForm should process the data as if it were posted, but neither that nor anything else I’ve set so far seems to do the trick.

    I’ve been trying to make this work without modifying the eForm file itself, but the clients are getting testy and I may just add my own flag in there for when I need to pre-populate the form.

    Unless anyone else can tell me how to do this some other way...?
      "Things are not what they appear to be; nor are they otherwise." - Buddha

      "Well, gee, Buddha - that wasn't very helpful..." - ZAP

      Useful MODx links: documentation | wiki | forum guidelines | bugs & requests | info you should include with your post | commercial support options
      • 33372
      • 1,611 Posts
      Well I gave up and made a slight modification to the eForm file in order to pre-populate the form. I’d still like to do it the "right" way, but at least this seems to be working. It wasn’t easy, so I’m going to share my code here in case anyone else ever struggles with something similar:

      /* function called by first form */
      // pass entered info to next form
      // runs on eFormOnBeforeMailSent event
      
      function passInfo( &$fields ){
      
         global $modx;
      
         $_SESSION['start']='start';
         $_SESSION['startemail']=$fields['email'];
         $_SESSION['prepopulate']='yes';
      
         $appdocid=434; //id of application page
         $apppage= $modx->makeUrl($appdocid);
         return $modx->sendRedirect($apppage);
      
      }


      /* functions called by second form */
      // get saved info from database
      // runs on eFormOnBeforeFormMerge event
      
      function populateData( &$fields ){
         global $modx;
         if ($_SESSION['start']=='start'){
            $fields['email'] = $_POST['email'] = strtolower($_SESSION['startemail']);
            $query = "SELECT * FROM modx_applications WHERE email='".$modx->db->escape($fields['email'])."' and status='unfinished'";
            $result = $modx->dbQuery($query);
            if(mysql_num_rows($result)!=1){ // Starting a new application - inserts the data, but doesn't populate the email field in the form
               $query = "INSERT INTO modx_applications SET email='".$modx->db->escape($fields['email'])."', status='unfinished', started=NOW(), modified=NOW()";
               $result = $modx->dbQuery($query);
               $fields['appid'] = $_POST['appid'] = mysql_insert_id();
            }
            else{ // Continuing a saved application
               $appInfo=mysql_fetch_array($result, MYSQL_ASSOC);
               foreach($appInfo as $key=>$value){
                  if($key!='status' && $key!='started' && $key!='modified'){
                     $fields[$key] = $_POST[$key] = $appInfo[$key];
                     //echo '<p>'.$key.' - '.$value.'</p>';  // I can tell by echoing this data that the database info is being recalled correctly
                  }
               }
            }
         $_SESSION['start']='';
         $_POST['formid']='application';
         }
         return true;
      }
      
      // function to store submitted data in the database
      // runs on eformOnValidate event
      
      function updateDatabase( &$fields, &$vMsg, &$rMsg ){
         global $modx;
         global $_lang;
         $fields['email'] = strtolower($fields['email']);
         foreach($fields as $key=>$value){
            $fields[$key] = str_replace('"', '"', $value);
         }
         if ($_SESSION['prepopulate']=='yes'){
               $fields['validationmessage']='';
               $vMsg=array(' ');
               $rMsg=array();
               $_lang["ef_validation_message"]='';
               $_SESSION['prepopulate']='no';
         }
         elseif ($fields['save']=='save'){
            if ( !preg_match('/^(?:[a-z0-9_-]+?\.)*?[a-z0-9_-]+?@(?:[a-z0-9_-]+?\.)*?[a-z0-9_-]+?\.[a-z0-9]{2,5}$/i', $fields['email']) ) {
            $fields['validationmessage']='You must enter a valid email address in order to save your unfinished application. You will need it to retrieve your information later.';
            $vMsg=array(' ');
            $rMsg=array();
            $_lang["ef_validation_message"]='';
            }
            else{
            $query = 'UPDATE modx_applications SET ';
               foreach($fields as $key=>$value){
                  if($key!='formid' && $key!='appid' && $key!='send' && $key!='save' && $key!='savebutton' && $key!='validationmessage'){
                     $query .= $key.'=\''.$modx->db->escape($value).'\', ';
                     // echo '<p>'.$key.' - '.$value.'</p>';
                  }
               }
            $query .= 'status=\'unfinished\', ';
            $query .= ' modified=NOW()';
            $query .= ' WHERE appid=\''.$fields['appid'].'\'';
            $result = $modx->dbQuery($query);
            $fields['validationmessage']='<h3>Your information has been saved.</h3>';
            $vMsg=array(' ');
            $rMsg=array();
            $_lang["ef_validation_message"]='';
            }
         }
         return true;
      }
      
      // saves final info into database
      // runs on eFormOnMailSent event
      
      function finalizeDatabase( &$fields ){
         global $modx;
         $query = 'UPDATE modx_applications SET ';
            foreach($fields as $key=>$value){
               if($key!='formid' && $key!='appid' && $key!='send' && $key!='save' && $key!='savebutton' && $key!='validationmessage' && $key!='postdate' && $key!='disclaimer' && $key!='subject'){
                  $query .= $key.'=\''.$modx->db->escape($value).'\', ';
                  // echo '<p>'.$key.' - '.$value.'</p>';
               }
            }
         $query .= 'status=\'complete\', ';
         $query .= ' modified=NOW()';
         $query .= ' WHERE appid=\''.$fields['appid'].'\'';
         $result = $modx->dbQuery($query);
         return true;
      }
      


      /* minor change to eform.inc.php at line # 141 (eForm 1.4.1) */
      
      	if ($isPostBack || $_SESSION['prepopulate']=='yes') {
      
      


      Oh - I had to replace quotes in my form input in order not to break their values when the form is validated and returned for more input. That seems strange to me, since I would’ve figured that eForm took care of this (it does everything else). Is this a minor bug, or am I missing something?
        "Things are not what they appear to be; nor are they otherwise." - Buddha

        "Well, gee, Buddha - that wasn&#39;t very helpful..." - ZAP

        Useful MODx links: documentation | wiki | forum guidelines | bugs & requests | info you should include with your post | commercial support options
        • 33372
        • 1,611 Posts
        So I still don’t know how to do this without modifying the eForm snippet. And of course using session vars is kind of an amateurish way to bridge this gap. But it does work anyway...

        If there isn’t a way currently to prepopulate form fields using eForm, then I suggest that be added to the future version to-do list. Perhaps in addition to the $fields array, there might be a use for an $eform_commands array that would allow for overrides of the typical logic and behavior of the snippet from event-triggered functions. So for example one of these might be $eform_commands[’force_postback’], and if that were set to true the snippet would run the post-back segment of the code, which would allow you to pre-populate the form.

        I need to do this for several forms now, so I guess I’ll use my hacked version until the new official version includes this (or until someone explains how to do it with the current version).
          "Things are not what they appear to be; nor are they otherwise." - Buddha

          "Well, gee, Buddha - that wasn&#39;t very helpful..." - ZAP

          Useful MODx links: documentation | wiki | forum guidelines | bugs & requests | info you should include with your post | commercial support options
          • 34017
          • 898 Posts
          Zap,

          I was looking into this with Anders ppp function.

          All it really does is update a table for a specific user with items from eForm.
          I modified it to allow anonymous submissions.

          Then, I was planning on creating 3 forms
          step1
          step2
          step3

          You could place some links to Step 1 - Step 2 - Step 3 at the top of the form.

          I am looking at adding some functions to ppp to make it a basic CRUD tool via eForm. I am sure it can be worked to look for a certain field and forward.

          - What about creating a field called "StepCompleted".
          - Create a hidden input for StepCompleted with the value of the step you are on "1", "2", "3".
          - Modify ppp where (pseudo code):
          if StepCompleted = '2' 
              then header: location 'step3-page', 
          else if StepCompleted = '1' 
              then header: location 'step2-page', 
          else 
              header: location 'step1-page'

          - I believe you could put this in the function "populate" since it is called before eForm is shown.

          Chuck
            Chuck the Trukk
            ProWebscape.com :: Nashville-WebDesign.com
            - - - - - - - -
            What are TV's? Here's some info below.
            http://modxcms.com/forums/index.php/topic,21081.msg159009.html#msg1590091
            http://modxcms.com/forums/index.php/topic,14957.msg97008.html#msg97008
            • 33372
            • 1,611 Posts
            I would think something like that should work, but in practice I can’t get eForm to treat a fresh form as if it’s been posted (perhaps I need to refresh the page with the post values?)...
              "Things are not what they appear to be; nor are they otherwise." - Buddha

              "Well, gee, Buddha - that wasn&#39;t very helpful..." - ZAP

              Useful MODx links: documentation | wiki | forum guidelines | bugs & requests | info you should include with your post | commercial support options
              • 34017
              • 898 Posts
              Quote from: ZAP at Dec 06, 2006, 09:24 AM

              but in practice I can’t get eForm to treat a fresh form as if it’s been posted (perhaps I need to refresh the page with the post values?)...

              i didnt understand this.

              Chuck
                Chuck the Trukk
                ProWebscape.com :: Nashville-WebDesign.com
                - - - - - - - -
                What are TV's? Here's some info below.
                http://modxcms.com/forums/index.php/topic,21081.msg159009.html#msg1590091
                http://modxcms.com/forums/index.php/topic,14957.msg97008.html#msg97008
                • 33372
                • 1,611 Posts
                Quote from: ProWebscape at Dec 06, 2006, 11:05 AM

                Quote from: ZAP at Dec 06, 2006, 09:24 AM

                but in practice I can’t get eForm to treat a fresh form as if it’s been posted (perhaps I need to refresh the page with the post values?)...

                i didnt understand this.

                Chuck

                The form doesn’t populate (show values) unless eForm can be made to think that it has been posted. This is because eForm is now super-cool in that it adds value="[+placeholder+]" to each form field (as well as selecting and checking the appropriate ones), but only AFTER the form has been posted. So even if you set the $fields or $_POST values in a function triggered by an event that happens before the form is parsed, these values are not inserted into the form.

                So there needs to be some other trigger to force eForm to parse the form without a post. I used a session variable (because I was setting it from another page), but it seems like eForm itself could have a variable that does this instead. And that made me think that there are probably other occasions when someone might want to change the usual logic of eForm, and that perhaps there could be some $eform_commands variables that allow people to do that (e.g., if the condition is met OR this command var is set, run this function).

                There are probably ways to do this by actually re-posting the page also, which was what I meant in parentheses above...
                  "Things are not what they appear to be; nor are they otherwise." - Buddha

                  "Well, gee, Buddha - that wasn&#39;t very helpful..." - ZAP

                  Useful MODx links: documentation | wiki | forum guidelines | bugs & requests | info you should include with your post | commercial support options
                  • 30223
                  • 1,010 Posts
                  @ZAP, I haven’t been following this thread but have you seen topic? with an example for multi page forms?
                    • 33372
                    • 1,611 Posts
                    Quote from: TobyL at Dec 06, 2006, 06:48 PM

                    @ZAP, I haven’t been following this thread but have you seen topic? with an example for multi page forms?


                    I have seen that thread. Maybe I could’ve used something like this technique if my two forms were actually on one page (and there’s no reason they couldn’t be). The key was that I needed to be posting to the page in order to populate the form, but perhaps by using two form templates as in your example it would do what I need.

                    Now the trick will be in forms where you need to go both backwards and forwards (previous as well as next links). I guess I could just change the step value with JavaScript and this.form.submit(). Something to try for the next one...

                    Incidentally, my second form is a multi-page form using tab panes (like the MODx Manager in for example the Resources page). That makes it all one form when submitted, so programatically it’s easy. But I kind of like validating step-by-step on the server, so I’ll try your method next.
                      "Things are not what they appear to be; nor are they otherwise." - Buddha

                      "Well, gee, Buddha - that wasn&#39;t very helpful..." - ZAP

                      Useful MODx links: documentation | wiki | forum guidelines | bugs & requests | info you should include with your post | commercial support options
                      • 34017
                      • 898 Posts
                      Quote from: ZAP at Dec 06, 2006, 05:12 PM


                      The form doesn’t populate (show values) unless eForm can be made to think that it has been posted. This is because eForm is now super-cool in that it adds value="[+placeholder+]" to each form field (as well as selecting and checking the appropriate ones), but only AFTER the form has been posted. So even if you set the $fields or $_POST values in a function triggered by an event that happens before the form is parsed, these values are not inserted into the form.

                      So there needs to be some other trigger to force eForm to parse the form without a post.

                      I was suggesting actually submitting each step of the form then moving to a new form. This way the information would be stored in the database, if you wanted the info there. tobys idea is probably best here.

                      Chuck
                        Chuck the Trukk
                        ProWebscape.com :: Nashville-WebDesign.com
                        - - - - - - - -
                        What are TV's? Here's some info below.
                        http://modxcms.com/forums/index.php/topic,21081.msg159009.html#msg1590091
                        http://modxcms.com/forums/index.php/topic,14957.msg97008.html#msg97008