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

    I’m trying to make a loan calculator using eform
    I dont need to send any mail, just get the params, validate them and display some calculation result

    I’m using &eformOnBeforeMailSent=`calcMe` to set a custom placeholder

    Everything is almost perfect except... I can’t find a way to display the form again on that ’thankyou’ page to let a user change some params and re-run the process.

    Here’s what I need:

    1. Original page
    ---
    param1: [ ]
    param2: [ ]
    [ calculate ]
    --

    2. "Thankyou" page
    ---
    param1: [ POST value ]
    param2: [ POST value ]
    [ calculate ]
    --
    Some result: XYZ
    -------------

    I can simulate the form on a "thankyou" page but there will be no validation etc.

    Any ideas?
      • 30223
      • 1,010 Posts
      Here’s a scenario you could try. Instead of using the &thankyou parameter use the &eformOnValidate event and the &eFormOnBeforeFormMerge event to do the following.

      In the eFormOnValidate event function you can check if there are any validation errors, if so just let eForm handle things as usual. If the form validates run your calculation and then set a dummy error in the $vMsg array. This will cause eForm to think there’s an error and so display the form again. However you don’t want an
      error message to appear so, in the eFormOnBeforeFormMerge event function you check if the special dummy error is set by checking the $fields[’validationmessage’]. If so you delete the $fields[’validationmessage’] and let eForm display the form. (Sadly you can’t check the $vMsg array directly as it is not available in this event function)

      <?php
      function onValidateFunction(&$fields,&$vMsg,&$rMsg){
         
         if( count($vMsg)+count($rMsg)>0 ) return true; //let eForm handle true errors
         
         $rMsg[] = 'myDummyFlag'; //force an erorr...
         calcMe(&$fields); //call original calculation function
         return true;
      
      }
      ?>


      <?php
      function onBeforeFormMergeFunction(&$fields){
         
         if(strstr($fields['validationmessage'],'myDummyFlag')
            unset($fields['validationmessage']);
         
         return true;
      }
      ?>


      You will of course need to place a place holder for your calculation results in the form template itself. This may mean that you have to include a whole paragraph in the placeholder value. You can in fact put that in a separate chunk and load that in your ’calcMe’ function

      <?php
      function calcMe(&$fields){
         //do calculation
         
         $fields['result'] = $fields['a'] * $fields['b'];
         
         //load template
         $result_template = efLoadTemplate('chunkName');
         $fields['result_message'] = formMerge($result_template,$fields);
         //
         return;
      }
      ?>

      example result chunk:

      <p>The result of [+a+] times [+b+] is [+$result+].</p>
      <p>Feel free to try again</p>


      Doing things this way does mean that the form will never officially validate and never reach any thank you page, but in your case that doesn’t matter I suspect.
        • 33357
        • 10 Posts
        Many thanks TobyL!

        Your code is already functional except for one ’)’ is missing!
        I guess it’s really useful in many scenarios so you could make it a howto.

        Once again – thank you!