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
    Quote from: sirlancelot at Jan 30, 2007, 05:11 PM

    Once you parse the data from one of your steps you need to clear the $_POST data so that eForm goes in to normal mode instead of validation mode. I struggled with this issue for quite sometime on a related issue.

    eForm detects the existence of $_POST data related to your form, since it still exists even AFTER you go to step 2, it will try to validate the data against step 2’s input. Clearing the $_POST data will solve the issue.

    Alternatively, you can [tt]unset($_POST[’formid’]);[/tt] if you don’t want to completely destroy the $_POST array.

    Yes, this is indeed the case! and something to be addressed in the example in the first post. I struggled with it just now trying this out again myself. Here the corrected functions for the example where the $_POST is cleared at the appropriate steps.


    <?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:
    			$replaceTpl = "multiForm2";
    			break;
    		case 3:
    			$replaceTpl = "multiForm3";
    			break;
    		default: //should never get here
    			return false;
    		}
    		if($step>1)
    			if( $tmp=efLoadTemplate($replaceTpl) ) $tpl=$tmp; else $tpl = $_lang['ef_no_doc'] . " '$replaceTpl'";
    
    		//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;
    		$debugText .= "<b>BeforeMailSent: step=".$step."</b><br />";
    		switch($step){
    		case 1: //save values in session
    			$_SESSION['name']= $fields['name'];
    			$_SESSION['email']= $fields['email'];
    			$_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['name'] = $_SESSION['name'];
    			$fields['email'] = $_SESSION['email'];
    			$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 "";
    ?>
    


    There is another complication however. eForm has evolved since the original example and you may find that the from never gets beyond the second form! Submitting the second form will simply display the same form again, without any values.
    This is because in the newer eform versions it is no longer required to place the formid in a hidden field. This field is normally added by eForm after reading the id from <form id="contact"...> but because we are swapping the form after this has happened eForm doesn’t see a proper formid and consequently doesn’t process the form.

    To overcome this problem make sure that all the forms in the sequence have a hidden field for the formid. For the example form this would be:
    <input type="hidden" name="formid" value="contact" />


    Hope this solves your problem.
      • 727
      • 502 Posts
      Now it works perfectly! Thanks! As long as I remove the eform parameter from the hidden field containing the step number. laugh

      Thanks TobyL and sirlancelot.

      Andy
        • 727
        • 502 Posts
        Everything was working great with 1.4.1 until I upgraded to 1.4.3. Now the placeholders [+formvalue+], etc. are not filled in. I want to show the user everything they have entered so they can confirm it and I did that with a form full of placeholders. That doesn’t seem to work now. Any ideas?

        Andy
          • 727
          • 502 Posts
          Any ideas how the CAPTCHA system can be integrated? I guess the easiest way would be to have the vericode input on the first "page" and then keep passing the vericode to each "page" for validation. In beforeMailSentFunction I tried several variations and they all failed. Here is my latest:

            function beforeMailSentFunction(&$fields) {
              global $modx;
              $step = isset($fields['formStep']) ? $fields['formStep'] : 1;
          
              switch ($step) {
                case 1:
                  $_SESSION['foo'] = $fields['foo'];
                  $_SESSION['vericode'] = $fields['vericode'];
                  $_SESSION['temp_veriword'] = $_SESSION['veriword'] ? $_SESSION['veriword'] : $_SESSION['eForm.VeriCode'];
                  break;
                case 2:
                 $_SESSION['bar'' = $fields['bar'];
                 $fields['vericode'] = $_SESSION['vericode'];
                 $_SESSION['veriword'] = $_SESSION['temp_veriword'];
                 break;
              }  
          


          When I tried to submit the second "page", it gives an "Invalid verification code" error.

          Any ideas on how this can be made to work?

          Andy
            • 30223
            • 1,010 Posts
            Any ideas how the CAPTCHA system can be integrated? I guess the easiest way would be to have the vericode input on the first "page" and then keep passing the vericode to each "page" for validation. In beforeMailSentFunction I tried several variations and they all failed. Here is my latest:
            Isn’t it more logical to have the vericode field on the last form only? The aim of CAPTCHA is to make sure an actual person is submitting the form and not some scripted bot. I makes sense to me to only do that when all the info is supplied.

              • 727
              • 502 Posts
              Hmmm, so all I have to do for every "page" except the last, is to make sure that $fields[’vericode’] and $_SESSION[’veriword’] match some arbitary value?

              Andy
                • 30223
                • 1,010 Posts
                Ah, I see your problem... I’m a bit slow smiley You have to set &vericode=`1` off course.

                I guess the easiest would be to populate the $fields[’vericode ’] in the eFormOnBeforeFormParse event for the first few forms. The vericode is set in a session variable so you could do this for all but the last form:

                $fields['vericode'] = $_SESSION['eForm.VeriCode']
                


                  • 727
                  • 502 Posts
                  Yes, that worked with a slight modification, thanks!

                  To confirm:

                  In the snippet call set vericode=`1`
                  In beforeParseFunction set:

                  $_SESSION['veriword'] = "";
                  $fields['vericode'] = $_SESSION['eForm.VeriCode'];


                  for every step except the last.
                  In the template for the last step include the vericode/CAPTCHA HTML as usual.

                  Andy
                    • 727
                    • 502 Posts
                    My only remaining problem is this:

                    In my form I have a bunch of fields, for example foo, bar and baz.
                    The template for my last step looks like this:

                    <form method="post" action="[~[*id*]~]" id="datainput">
                    <input type="hidden" name="formStep" value="3"/>
                    <p>Please confirm your entry:<br/>
                    Item 1: [+foo+]<br/>
                    Item 2: [+bar+]<br/>
                    Item 3: [+baz+]<br/>
                    <input type="submit"  name="go" value="finish"/></form>
                    


                    The idea is that the last page shows the user everything they have entered so far.

                    With eForm 1.4.1 this worked. With eForm 1.4.3 the placeholders are replaced with blanks rather than the form data.

                    Any ideas on how I can patch eForm to fix this?

                    Andy
                      • 30223
                      • 1,010 Posts
                      First of all aren’t you are missing hidden fields for these values? i.e.
                      <form method="post" action="[~[*id*]~]" id="datainput">
                      <input type="hidden" name="formStep" value="3"/>
                      <input type="hidden" name="foo" value="[+foo+]"/>
                      <input type="hidden" name="bar" value="[+bar+]"/>
                      <input type="hidden" name="baz" value="[+baz+]"/>
                      <p>Please confirm your entry:<br/>
                      Item 1: [+foo+]<br/>
                      Item 2: [+bar+]<br/>
                      Item 3: [+baz+]<br/>
                      <input type="submit"  name="go" value="finish"/></form>


                      Ehm, I suppose that isn’t necessary as you’ve got the values in $_SESSION. dooh,...

                      As long as you’ve re-populated the $fields array before you arrive at this I see no reason why it shouldn’t work...
                      <?php
                        $fields['foo'] = $_SESSION['foo'];
                        $fields['barl'] = $_SESSION['barl'];
                        $fields['baz'] = $_SESSION['baz'];
                      ?>