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

    I’m looking to pass a variable created from the &eFormOnBeforeFormMerge event to the function called by &eFormOnBeforeMailSent, whats the best way to achieve this?

    I’m quite new to php so any suggestions appreciated.

    Cheers,

    Steve

    PS I’ve included both functions in the same snippet if this makes a difference

    EDIT:

    I’ve just looked into this a little more and realised that this may be slightly more tricky than I originally throught.

    The initial snippet requests a unique key from an external source, it is this key i wish to pass on form submit. However would i be correct in thinking that if the form fails validation, the &eFormOnBeforeFormMerge event will be called again - thus causing a new unique key to be requested?

    Equally initially a small flash applet is loaded, is there a way to preclude this from loading again if the form fails validation on submit?

    Is there a way around this?

    All help appreciated as I am now very confussed lol!
      • 30223
      • 1,010 Posts
      The order in which events are called are as follows:

      The first time the form is displayed:
      eFormOnBeforeFormParse
      eFormOnBeforeFormMerge

      On successful submit
      eFormOnBeforeFormParse
      eFormOnValidate
      eFormOnBeforeMailSent
      eFormOnMailSent

      On failed submit (failed validation)
      eFormOnBeforeFormParse
      eFormOnBeforeFormMerge

      The events you mention are mutually exclusive, meaning that they are not both called withing the same page request, thus you are trying to pass a variable from one page request to another page request . Isn’t that what forms are good for? The simplest way would appear to be to create a hidden field in your form which is filled with the unique key by the event function. However, the eFormOnBeforeFormMerge isn’t the best place to do this as you’ll run into validation problems later on. You could use the eFormOnBeforeFormParse instead, that is if you can guarantee creating the same key between page requests.

      If creating the same (unique) key may proof difficult or impossible you can abandon the hidden field and instead use a session variable. In your event function you first check if the session variable exists and only if not, create one.
      <?php
      If( !isset($_SESSION['mySessionVar']) ) $_SESSION['mySessionVar'] = createMyUniqueKey();
      ?>

      As the $_SESSION is a super global you have access to it in any of your subsequent event functions.

      As for the flash applet, yes you can. Create a place holder in your form where you want flash applet and use the eFormOnBeforeFormParse and eFormOnValidate events.

      <?php
      function OnBeforeFormParse(&$fields, &$templates){
          //set place holder value to the flash markup
          $fields['myFlashApplet'] = "<object ...>all your flash html markup here</object>";
          return true;
      }
      
      function OnValidateFunction(&$fields, &$vMsg, &$rMsg ){
          //if there are errors simply remove the flash markup
          if(count($vMsg)>0 || count($rMsg)>0) unset($fields['myFlashApplet']);
          return true;
      }
      ?>
      
        • 6437
        • 157 Posts
        Great reply, thanks TobyL, I was looking for a nice breakdown on when the eForm events were called. I’ll try to put this into a flow diagram and add it to the Wiki.

        I was originally planning on using a hidden field, although I think i’ll go with the $_SESSION option instead.

        Good tip on unsetting the place holder, I didnt realise that was possible.

        Thanks!

        Steve
          • 30223
          • 1,010 Posts
          Just a reminder... be aware that the session variable will remain as long as the session is valid so you may want to unset the session variable as soon as the form is submitted successfully. You could do this in the eFormOnBeforeMailSent or eFormOnMailSent event.
            • 6437
            • 157 Posts
            That tripped me up for a short while TobyL, made worse by the fact that I took the concept a little further and tried setting an additional session variable from an iframe called by javascript when an event on the page is completed.

            All working now though, thanks for your help.

            Cheers,

            Steve