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

    I’ve rebuilt our tennis club site http://tlaer.nl with Modx. For a tournament, I would like to handle all subscriptions through eForm. I’ve implemented the multi page example, which works fine (small css issue aside). My subscription should also be split into three parts. The first form would contain general info, including age and gender. The second form would be a dropdown list, for which the contents depend on the age and gender.
    Now, how would I go about implementing this? It would work (I think) if I can add a snippet as form and use the same beforeParsefunction.

    Anyone? Thanks.
      • 30223
      • 1,010 Posts
      You can extend the beforeParseFunction and let it build your form dynamically. You wouldn;t need to have a separate snippet for that although you could if that makes it easier. Just run the ’form snippet’ from the beforeParseFunction at the right spot.

      For example:

      <?php
      if(!function_exists("beforeParseFunction")){
        //Display different form depending on session
        function beforeParseFunction( &$fields, &$templates ){
          global $_lang;
          //which form should we load?
          // $step is set in beforeMailSentFunction or comes from the hidden field
          // we need to do both so that when we step back (eg. the back button of the browser)
          // we still get the proper form
          if(isset($_SESSION['formStep']))
            $step = $_SESSION['formStep'];
          else
            $step = isset($_POST['formStep'])?$_POST['formStep']:1;
          //Kill the session var as otherwise we can't go back to a previous step at all
          unset($_SESSION['formStep']);
          $tpl = $templates['tpl'];
          // Replace form template when needed
          switch($step){
          case 1: //form not yet posted so use tpl as set in snippet call
            return true;
          case 2:
      		//build 2nd form using a snippet      
      		$tmp = $modx->runSnippet('makeSecondForm',array('age'=>$fields['age'], 'gender'=>$fields['gender']) );
      		$tpl = ($tmp) ? $_lang['ef_no_doc'] . " 'second Form'";
            break;
          case 3:
            if( $tmp=efLoadTemplate('multiForm3') ) $tpl=$tmp; else $tpl = $_lang['ef_no_doc'] . " '$replaceTpl'";
            break;
          default: //should never get here
            return false;
          }
           //restore template in array
          $templates['tpl']=$tpl;
          return true;
        }
      }
      ?>


      The ’makeSecondForm’ snippet will have to return the complete form or false if an error occurs. This is only one of several ways to achieve this (as usual with MODx) I’ll let you work out the rest yourself.
        • 26482
        • 138 Posts
        Easier is better, building the form dynamically seems easier. Can I assume the $templates var contains all chunks? Are these chunks just strings? Could you possibly give a tiny example how such a dynamic chunk could be created? That would be of great help. Thanks.
          • 30223
          • 1,010 Posts
          Oops, was just in the process of editing the first reply ... see above smiley

          The $templates var contains all the chunks yes. See the eForm documentation for the events which these are.
            • 26482
            • 138 Posts
            Hmm...I really need your help here. This is what I’ve done sofar.
            Snippet to generate second form
            <?php
            echo <<<END
            <!-- Categories chunk -->
            <h3>Starting: step 2 of 3</h3>
            <p class="error">[+validationmessage+]</p> 
            <form method="post" action="[~[*id*]~]" id="contact">
              <fieldset>
                <input type="hidden" name="formid" value="contact" />
                <input type="hidden" name="formStep" value="2" />
                <p><select name="onderdeel">
            			<option value="0">Selecteer onderdeel</option>
            			<option value="193693">HD6</option>
                                    <option value="193691">DD5</option>
            		</select>
            </p>
                <input type="submit" name="go"  value="Next" /></p>
              </fieldset>
            </form>
            END;
            ?>
            


            When called separately, this snippet outputs a dropdown box, as expected (I haven’t touched on using the age and gender variables here).

            I’ve copied your code, see below, and added the beforeMailSent function.
            <?php
            if(!function_exists("beforeParseFunction")){
              //Display different form depending on session
              function beforeParseFunction( &$fields, &$templates ){
                global $_lang;
                //which form should we load?
                // $step is set in beforeMailSentFunction or comes from the hidden field
                // we need to do both so that when we step back (eg. the back button of the browser)
                // we still get the proper form
                if(isset($_SESSION['formStep']))
                  $step = $_SESSION['formStep'];
                else
                  $step = isset($_POST['formStep'])?$_POST['formStep']:1;
                //Kill the session var as otherwise we can't go back to a previous step at all
                unset($_SESSION['formStep']);
                $tpl = $templates['tpl'];
                // Replace form template when needed
                switch($step){
                case 1: //form not yet posted so use tpl as set in snippet call
                  return true;
                case 2:
            		//build 2nd form using a snippet      
            		$tmp = $modx->runSnippet('eFormCategories',array('age'=>$fields['age'], 'gender'=>$fields['gender']) );
            		$tpl = ($tmp) ? $_lang['ef_no_doc'] . " 'FormTwo'";
                  break;
                case 3:
                  if( $tmp=efLoadTemplate('FormThree') ) $tpl=$tmp; else $tpl = $_lang['ef_no_doc'] . " '$replaceTpl'";
                  break;
                default: //should never get here
                  return false;
                }
                 //restore template in array
                $templates['tpl']=$tpl;
                return true;
              }
            }
            
            if(!function_exists("beforeMailSentFunction")){
              //store results in session and advance form
              function beforeMailSentFunction(&$fields){
                global $modx;
                $step = isset($fields['formStep'])?$fields['formStep']:1;
                switch($step){
                case 1: //save values in session
                  $_SESSION['age']= $fields['age'];
                  $_SESSION['gender']= $fields['gender'];
                  $_SESSION['formStep'] = 2; //prepare next step
                  break;
                case 2: //save values in session
                  $_SESSION['address']= $fields['address'];
                  $_SESSION['town']= $fields['town'];
                  $_SESSION['formStep'] = 3; //prepare next step
                break;
                case 3://last form
                  //from here we go to the report so we need to restore previous values
                  // into the fields array so they can be used in the report
                  $fields['age'] = $_SESSION['age'];
                  $fields['gender'] = $_SESSION['gender'];
                  $fields['address'] = $_SESSION['address'];
                  $fields['town'] = $_SESSION['town'];
                  return true; //return now and let eForm do it's thing
                default: //should never get here
                  return false;
                }
                // except for the last step we clear the $_POST array
                // and bypass eform and re-open the page with the next form
                unset($_POST);
                $modx->sendForward($modx->documentIdentifier);
                return false; //
              }
            }
            //return from snippet
            return "";
            ?>


            However, nothing is shown (modx gives an empty page, no errors). What have i done wrong? Also, is there a way to turn on debugging, so I can see what php errors I’ve made?

            Thanks!
              • 30223
              • 1,010 Posts
              Yes putting your snippet directly in a document would show, although this is more by accident then by good design... the eForm event function won’t see any of it as you’re not returning any value to it. By using echo you’re just trying to output something to the browser, instead of making a value available to a script. Pretty basic php stuff.

              What you need to do in your snippet is build up a variable and return that.

              <?php
              $output =  <<<END
              <!-- Categories chunk -->
              <h3>Starting: step 2 of 3</h3>
              <p class="error">[+validationmessage+]</p> 
              <form method="post" action="[~[*id*]~]" id="contact">
                <fieldset>
                  <input type="hidden" name="formid" value="contact" />
                  <input type="hidden" name="formStep" value="2" />
                  <p><select name="onderdeel">
              			<option value="0">Selecteer onderdeel</option>
              			<option value="193693">HD6</option>
                                      <option value="193691">DD5</option>
              		</select>
              </p>
                  <input type="submit" name="go"  value="Next" /></p>
                </fieldset>
              </form>
              END;
              
              return $output;
              ?>


              I do hope your snippet is going to do a bit more then that though, otherwise you can just as easily use chunks...
                • 26482
                • 138 Posts
                Quote from: TobyL at Feb 08, 2007, 09:36 AM

                I do hope your snippet is going to do a bit more then that though, otherwise you can just as easily use chunks...

                LOL. It will do, after I get the basics nailed down.

                Btw, the snippet itself (as posted above), doesn’t run. This is due to the following line:
                $tpl = ($tmp) ? $_lang['ef_no_doc'] . " 'FormTwo'";

                If I comment out this line, the script runs. However, when I press ’Next’ on FormOne, I receive the empty page.

                Is there a way to enable debugging?

                Thanks!
                  • 30223
                  • 1,010 Posts
                  That should have been:
                  $tpl = ($tmp) ? $tmp : $_lang['ef_no_doc'] . " 'FormTwo'";
                  


                  Commenting out that line doesn’t mean it works. It just means you’re ignoring an error. As $tpl doesn’t get a value when commenting out that line it’s no wonder you’re getting an empty screen!

                  And yes, you can enable debugging. It’s all in the eForm documentation, which off course you’ve read extensively... set the &debug parameter to 1,2 or 3 for different levels of debugging. However that wouldn’t have helped you much in this case.
                    • 26482
                    • 138 Posts
                    Quote from: TobyL at Feb 08, 2007, 05:32 PM

                    That should have been:
                    $tpl = ($tmp) ? $tmp : $_lang['ef_no_doc'] . " 'FormTwo'";
                    


                    Thanks. I found that one myself. Also, the reason why the original code wasn’t working, is because there is no global $modx in the beforeParseFunction. Adding that solved most problems.

                    Quote from: TobyL at Feb 08, 2007, 05:32 PM

                    And yes, you can enable debugging. It’s all in the eForm documentation, which off course you’ve read extensively... set the &debug parameter to 1,2 or 3 for different levels of debugging. However that wouldn’t have helped you much in this case.
                    Actually, I’ve read the documentation quite extensively. The debug parameter should be "Set to 1 for extended error messages in form validation", which I wasn’t looking for. My main issues were 25 million miles before that (switching on php error reporting fixed this).

                    I’ve now successfully implemented the second form, whereby the contents change with the age and gender fields.

                    Now, on my localhost implementation (where I’m doing this), I still had eForm1.4.1 and the solution works (partially, at least the interdependency between forms). After I upgraded to 1.4.3, it doesn’t work anymore, because the fields aren’t passed through to the snippet any longer. Does that sound familiar? I’ve read that the 1.4.3 release was specifically for multiple forms. With the 1.4.3 version, the parameters aren’t working, but eForm is outputting the final FormReport, something that doesn’t work with 1.4.1.

                    Jelle, thank you for your help. I’m not a total php-noob (feel free to disagree!), I just need some handholding through the darkness that is Modx sometimes...
                      • 30223
                      • 1,010 Posts
                      Post your snippets, snippet calls and your form templates and I’ll give it a try here locally. Zip them up as an attachment if you like.