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

    I have been trying to figure out how to redirect to a certain page regarding a radio button selected.
    I have been searching the forum for days, but I didn't find any thread that really answer this question.

    Sorry to say that, but the documentation about it ( http://rtfm.modx.com/display/ADDON/FormIt.Hooks.redirect ) is too short and lack of proper examples for a newbie.


    Does anybody knows a simple way to redirect people on different page regarding what they enter into the form ?


    Thanks.
      Webdesigner and musician.
      • 10487 MODX Staff
      • 1,535 Posts
      If I'm understanding correctly, then the built-in redirect hook will not provide the functionality you require. The best approach is to create a custom post-hook that you can use to tailor your redirects based on which radio option is selected.

      For example, something like this should work (with obvious tailoring to your needs) ...

      Add this as a snippet called 'formit.multiRedirect':

      <?php
      $myRadioButtonValue = $hook->getValue('myRadioButton');
      switch ($myRadioButtonValue) {
          case 'option1':
              $url = $modx->makeUrl(1);
              $modx->sendRedirect($url);
              break;
          case 'option2':
              $url = $modx->makeUrl(2);
              $modx->sendRedirect($url);    
              break;
          default: /* no option selected */
              $url = $modx->makeUrl(3);
              $modx->sendRedirect($url);    
              break;
      }
      
      return;

      And then add it to your FormIt hook chain in place of the default 'redirect' hook.

      Hope that helps smiley
        Garry Nutting
        Senior Developer
        MODX, LLC

        Email: [email protected]
        Twitter: @garryn
        Web: modx.com
        • 34197
        • 138 Posts
        Tks Garry, I'll try that straight away.
          Webdesigner and musician.
          • 34197
          • 138 Posts
          Hi Gary, I have tried what you just advised me, but I think I am missing something. The page is not redirecting, actually it is redirecting to the same page, though the form works, and I am receiving an email.


          Here is my formit:

          [[!FormIt? &hooks=`spam,email,formit.multiRedirect` &emailTpl=`formEmailTpl` &emailSubject=`My subject` &emailTo=`[email protected]`  &redirectTo=`1` ]]
          
          <form action="[[~[[*id]]]]" method="post">
          
          <table width="100%">
          <tbody>
          <tr>
          <td><label>myRadioButton: [[!+fi.error.myRadioButton]]</label><input type="hidden" name="myRadioButton[]" value="" /></td>
          <td width="80px"><label> <input id="option1" type="radio" name="myRadioButton[]" value="option1" checked="checked" /> option1</label></td>
          <td width="80px"><label> <input id="option2" type="radio" name="myRadioButton[]" value="option2" /> option2</label></td>
          <td><label> <input id="mademoiselle" type="radio" name="myRadioButton[]" value="mademoiselle" /> Mlle.</label></td>
          </tr>
          </tbody>
          </table>
          <input class="greenbutton" type="submit" name="submit" value="Suivant" />
          </form>
          


          The snippet is the same you mentioned.

          I am sorry to bother you, do you know what I am doing wrong ?


          Thanks again
            Webdesigner and musician.
            • 10487 MODX Staff
            • 1,535 Posts
            I would hazard a first guess that it's because you are actually passing your myRadioButton field as an array on the form POST.
            name="myRadioButton[]"

            As you're using radio buttons, you don't need to do that (you would if you were using checkboxes). So, if you change your radio option fields to the following then I suspect it'll work:
            name="myRadioButton"
              Garry Nutting
              Senior Developer
              MODX, LLC

              Email: [email protected]
              Twitter: @garryn
              Web: modx.com
              • 34197
              • 138 Posts


              THANK YOU SO MUCH !!

              I have been looking for days to make this thing works. Thanks again for your time.
                Webdesigner and musician.
                • 3866 ☆ A M B ☆
                • 350 Posts
                Hi Garry,

                I've made use of this snippet to redirect a form based on radio button selected, however, I want to store information from one page to the next.

                My call is:

                [[!FormIt? &submitVar=`go` &store=`1` &hooks=`ReferralRedirect` &vTextRequired=`All fields must be completed` &validate=`firstname:required,lastname:required`  ]] 


                The form info:

                      <form class="form" action="[[~[[*id]]]]" method="post">
                
                  <div class="row">
                <fieldset>  
                  <h2>Referrer Details</h2>
                    <div class="large-6 columns">
                        <label>First Name:* [[!+fi.error.firstname]]</label>
                      	<input type="text" id="firstname" name="firstname" value="[[!+fi.firstname]]" placeholder="First name" />
                	</div>
                  	
                  	<div class="large-6 columns">
                    	<label>Last Name:* [[!+fi.error.lastname]]</label>
                  		<input type="text" id="lastname" name="lastname" value="[[!+fi.lastname]]" />
                  	</div>
                	
                <h2>Select referral type to continue:</h2>       
                    <div class="large-12 columns">
                      <input type="hidden" name="referral" value=""/>
                      <input type="radio" name="referral" value="self"><label>Self Referral</label> <br/>
                      <input type="radio" name="referral" value="professional"><label>Professional Referral</label>
                    </div>    
                    <div class="large-12 columns">
                        <input type="submit" value="Continue" name="go" class="form button"/> 
                    </div>
                 </fieldset>      
                </div>
                
                </form>


                And the snippet is ReferralRedirect:

                <?php
                $value = $hook->getValue('referral');
                switch ($value) {
                    case 'self':
                        $url = $modx->makeUrl(36);
                        $modx->sendRedirect($url);
                        break;
                    case 'professional':
                        $url = $modx->makeUrl(37);
                        $modx->sendRedirect($url);   
                        break;
                }
                 
                return;


                This works perfectly in so far as depending on the radio button selected the form redirects, but the firstname and last name is not stored.

                On the destination page for both I have:

                [[!FormItRetriever]]
                <p>[[!+fi.firstname]][[!+fi.lastname]]</p>


                But the information is being stored - suggestions?
                  Helen Warner
                  Number one pixel!
                  Proud to be linked with MODX at Crimson Pixel