We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 30223
    • 1,010 Posts
    As I said none was tested so possibly something is missing, however you’re making the wrong assumptions here!

    Your snippets [[testy]] and [[testy2]] run separate from eForm and thus can say nothing about what exists within eForm. The $templates and $fields array for instance exist only while eForm runs, not before and not afterwards. Testing it in separate snippets thus gives you no information about what’s going on. It is not suprising that they appear blank.

    To test anything in this case you’d have to do it from within the functions that eForm calls (or perhaps doesn’t call). Firstly you need to establish if eForm reaches the event functions at all. You could do this for instance by placing a simple die("I’m inside the " . __FUNCTION__); inside both functions.

    Please try this and see what happens.

    It appears that nothing happens.. It is thus highly likely that the onBeforeParseFunction is never called. Why? On closer inspection it turns out that I made a typo in the snippet call! &eFormOnBeforeParse should have been &eformOnBeforeFormParse. It really helps to have the documentation at hand and read and re-read the code as well as the documentation. Took me a while to spot it. (You can find the documentation in the snippets/eform/docs/ folder by the way)

    Having confirmed that the onBeforeParseFunction now works I found two more errors. The onBeforeMailSentFunction is not being called for a different reason. It seems that eForm doesn’t respond to a submitted form at all. It simple re-displays the form. Have a look at your formid in the snippet call and the form template. They are "download_form" & downloads_form. These need to be the same!! It is how eForm recognizes that posted values belong to this form.

    A last error is in the onBeforeMailSentFunction itself. It is missing a global $modx; statement, meaning that the function wouldn’t recognize $modx->config[’rb_base_dir’] an thus never be able to find the file and url.


    Programming is as much about analyzing what doesn’t work as it is about writing code that does work.

    Good luck. The rest is up to you.
      • 32645
      • 377 Posts
      Thank you so much for your help!

      I will look over your code, and analyse what you did -- and post my results (once successful).

      Thanks again for your help.
        • 32645
        • 377 Posts
        Hi,

        I’ve got most of it working now! But for some reason the page goes blank once I’ve submitted the form, and then it never sends anything.

        I’ve confirmed the following;

        1. The eform is getting the GET field, and displays it. The form works fine!

        2. All functions provided in the snippet above works fine, and it successfully grabs the file and puts it into my eform

        3. If you remove the "&eformOnBeforeFormParse=`onBeforeParseFunction` &eFormOnBeforeMailSent=`onBeforeMailSentFunction`" the eform will send an email without any problems, but if you put them back in -- it never sends anything.

        4. The problem is not in the functions but once its finished with the "onBeforeMailSentFunction" function.

        What’s happening is that when you press "Send" on the form, it redirects to the page -- but it never displays anything. I don’t know why.

        // The eform call
        [[eFormEventFunctions]]
        [!eForm?  &eformOnBeforeFormParse=`onBeforeParseFunction` &eFormOnBeforeMailSent=`onBeforeMailSentFunction` &tpl=`downloads_form` &formid=`downloads_form` &to=`[+email+]` &toname=`[+name+]` &from=`[email protected]` &report=`download_msg` &thankyou=`thankyou_for_download` &subject=`File download request` !]
        


        // eFormEventFunctions snippet
        
        <?php
        function onBeforeParseFunction(&$fields,&$templates){
        // die("I'm inside the :" . __FUNCTION__);
        
           global $modx;
           #If no file is selected redirect to another page (with download links?) 
          $pageId = 35; //change to id of whatever page you want to redirect to.   -- 35 is my downloads page, but this variable is never used?
        
           if(!isset($_REQUEST['file_id'])){ 
              $modx->sendRedirect($modx->makeUrl($pageId));  
              return false;
           }else{
              #store file_id in form so it propagates
              $templates['tpl'] = str_replace('[+file_id+]',$_REQUEST['file_id'],$templates['tpl']);
        
        /*
        This is where the problems start -- up to now, everything works fine -- but once it returns
        the page redirects to page 35 (via eform) -- but the page is blank.  If I remove
        all calls to this snippet and its functions it will redirect to page 35 with no problems and 
        displays a "thank you" message.  So there must be a problem with the way eform handles
        a redirect?
        */
        
              return true; //process normally
           }
        }
        
        function onBeforeMailSentFunction(&$fields){
        // die("I'm inside the " . __FUNCTION__);
        global $modx;
        
           #is called when form is submitted succesfully
           #file_id should now be in the fields array
        
           $file = '/assets/files/'.$fields['file_id'].'.pdf';
        
           #check that file exists
           if(!file_exists($modx->config['rb_base_dir']).$file){ 
               die("here");
              $value = ($modx->config['rb_base_dir']).$file);
              $fields['validationmessage']='The file requested ('.$file.') does not, or no longer exists!';
              return false;
           }else{
              #Get the url belonging to file_id and store in fields array
              # In your report template place a [+download_url+] place holder
              $url = $modx->config['rb_base_url'] . '/assets/files/'.$fields['file_id'].'.pdf';
              $fields['download_url'] = $url;
        
              return true;
           }
        }
        ?>
        


        And finally, the actual form

        <br clear="all" />
        
        <div id="validationmsg">[+validationmessage+]</div>
        
        <form method="post" action="/[~[*id*]~]" id="downloads_form">
        
        <input type="text" name="file_id" value="[+file_id+]" />
        <input type="hidden" name="formid" id="formid" value="downloads_form" />
        
        <p>
        <label for="name">Name:</label><br />
        <input type="text" name="name" id="name" maxlength="50" eform="Your name::1" />
        </p>
        
        <p>
        <label for="email">Your email address:</label><br />
        <input type="text" id="email" name="email" maxlength="40" eform="Your Email Address:email:1" />
        </p>
        
        <p>
        <input id="button" type="submit" class="submit" name="Submit" value="Send" />
        </p>
        
        </form>
        


        So in summary;

        When I press submit, it process everything fine -- then it redirects to page 35 and then the page goes blank. No error messages, nothing. If however I remove all calls to the "eFormEventFunctions" the eform sends an email, thank you message, etc without any problems.

        What am I doing wrong?
          • 32645
          • 377 Posts
          Update:

          It looks like it’s not even reaching the "&eFormOnBeforeMailSent=`onBeforeMailSentFunction`" part of my eform call.

          I put a die message in each function -- only on "onBeforeParseFunction" returns the message -- it never gets to "eFormOnBeforeMailSent".

          I’ve tried a lowercase/uppercase version of eform -- but in either case it never reaches it -- it simply submits the form and the page goes blank.

          [!eForm? &eformOnBeforeFormParse=`onBeforeParseFunction` &eFormOnBeforeMailSent=`onBeforeMailSentFunction` 
          


          I’m going to investigate further, but it sure is frustrating...
            • 32645
            • 377 Posts
            Update!

            I finally got it to work smiley

            I broke everything apart and found that it was not calling `onBeforeMailSentFunction` at all.

            I created a new function called "testthis" and found that although eform was going there, it was doing something that meant it either stopped processing, or never returned.

            I also found some other issues relating to the file path.

            Also the hyperlink generated was blank, it was trying to output the actual unix path rather than an actual hyperlink (which I fixed).

            Although I hacked it a bit, I did get it to work. And now it sends an email to the user with a link inter-twined.

            Everything’s the same as my previous post with exception of the function snippet.

            <?php
            function onBeforeParseFunction(&$fields,&$templates){
            // die("I'm inside the :" . __FUNCTION__);
            
               global $modx;
               #If no file is selected redirect to another page (with download links?) 
              $pageId = 35; //change to id of whatever page you want to redirect to.
            
               if(!isset($_REQUEST['file_id'])){ 
                  // $modx->sendRedirect($modx->makeUrl($pageId));  
                  print ('<p>You must select a file for download.');
                  return false;
               }else{
                  #store file_id in form so it propagates
                  $templates['tpl'] = str_replace('[+file_id+]',$_REQUEST['file_id'],$templates['tpl']);
                 // redirects somewhere
                  return true; //process normally
               }
            }
            
            /*
            This is a hack to get it to work and a send a URL to a user
            */
            function dothistest(&$fields) {
            global $modx;
            
            $file = 'files/'.$fields['file_id'].'.pdf';
            
            $actual_path = ($modx->config['rb_base_dir'].$file);
            
            if ( !file_exists($actual_path) )  { 
                    // print "FILE: " . ($modx->config['rb_base_dir']).$file);
                   // die("<br /><br />no file exists");
                  $fields['validationmessage']='The file requested ('.$file.') does not, or no longer exists!';
                  return false;
            } else {
              //     die(  "FILE: " . $file . " -- I'm inside the blah blah:  " . __FUNCTION__);
            
                  #Get the url belonging to file_id and store in fields array
                  # In your report template place a [+download_url+] place holder
            
                   // this doesn't output anything, or it sometimes output the unix path for some reason
                  // $url = $modx->config['rb_base_url'] . 'files/'.$fields['file_id'].'.pdf';
            
                 $url =  $modx->config['site_url']  . 'assets/files/'.$fields['file_id'].'.pdf';
            
                  // die ("URL: "  . $url );
            
                  $fields['download_url'] = $url;
                   
                  return true;
            } // end if
            
            } // end function
            ?>
            


            I want to thank everyone for their help, patience and diligence through this little nightmare. smiley

            Thanks again.