Conditional Redirect based upon radio button

mbajames 06 Feb, 2010
Hello,

I searched the forums and found items of a similar nature, but dealing with text fields. Does anyone know how to make a conditional redirect based upon the selection of a radio button?

Here is the case: I have a form, with conditional statements and validation(server side), where an important question is answered with a radio button "AYES_NACAT" where the possible values are "Yes" and "No". If a person chooses "No" I need to be able to send them to a different url once the form submits. A "Yes" answer should go to the redirect as stated in the main settings.

Any help is greatly appreciated.
GreyHead 06 Feb, 2010
Hi mbajames,

See a general example here you'll need to alter the first part to use the results from your radio buttons.

Bob
mbajames 06 Feb, 2010
Thank you for the link. That is the example I found and the one I have been trying to adapt.

Of course, I see to change ($name) as it is in your example to ($AYES_NACAT) for my situation, but I was getting lost on the other part.

However, after looking back at your replies to the initial question asker, I may have been using the wrong part...I may just have to use a sample like the last reply you made to them. I will try it, and as soon as I can get it to work will post the code in here so there is another reference (along with what had to be used to get the server side validation to work for my conditional form parts --and the quick server side way to validate a drop-down given that one of the drop down options is a "Please select from the list" with a value not equal to a legitimate answer).
mbajames 06 Feb, 2010
OK...I saw the error of my ways and part of it was not putting the code in the "onsubmit" box.

For anyone wanting to know, here is how I achieved the conditional redirect based upon a radio button (with large help to Bob's linked example):

In the "on Submit" box in the form code tab I entered
<?php
$radioname = JRequest::getString('radioname', 'value to go to normal post submit url', 'post');
if ($radioname == 'alternate value of radio') {
  $url = 'link taken to if alternate value of radio is selected';
} else {
  $url = 'link taken to if normal value of radio is selected';
}
$MyForm->formrow->redirecturl = $url;
?>


-----

When it comes to server side validating a drop down, the quick method is to assign an option value = 0 to the options and then to check to see that the field is not equal to zero:

if($_POST['fieldname'] == '0')
return 'Your error message to the viewer';


-----

To validate conditional fields I had to use a cascade of ANDs. For example, if field 2 is conditional of field 1, it is something like the following

if($_POST['field1'] == 'value' && $_POST['field2'] == 'value')
return 'message to viewer.';


If fields 2 and 3 are conditional on 1:

if($_POST['field1'] == 'value' && $_POST['field2'] == 'value' && $_POST['field3'] == 'value')
return 'You must provide the NAME of your school if it was not in the drop-down list.';


Obviously, where I have 'value' if you are checking to see if it is empty (nothing entered) it would just be ''

I hope this helps some people...
laurentmartin 16 Jun, 2011
Hi ,

I am a bit on the same situation and i don't know how to sort it out:

Typically I have 3 choice:
Choice 1
Choice 2
Choice 3

According to one of this choice, the user will be redirected to a predefined url


I enclose my code.

Can you advice how to:

1./ Redirect the user to the url accoridng to its choice
2./ How to have initial choice already checked in one of the choice




<div class="form_item">
  <div class="form_element cf_radiobutton">
    <label class="cf_label" style="width: 150px;">Filling type</label>
    <div class="float_left">
      <input value="Choice 1" title="" class="radio" id="radio00" name="radio0" type="radio" />
      <label for="radio00" class="radio_label">Choice 1</label>
      <br />
      
<input value="Choice 2" title="" class="radio" id="radio01" name="radio0" type="radio" />
      <label for="radio01" class="radio_label">Choice 2</label>
      <br />
      
<input value="Choice 3" title="" class="radio" id="radio02" name="radio0" type="radio" />
      <label for="radio02" class="radio_label">Choice 3</label>
      <br />
      

    </div>
    
  </div>
  <div class="cfclear"> </div>
</div>

<div class="form_item">
  <div class="form_element cf_button">
    <input value="Next" name="button_1" type="submit" />
  </div>
  <div class="cfclear"> </div>
</div>
GreyHead 26 Jun, 2011
Hi laurentmartin ,

Which version of CF are you using? For CF3.2 the code in the earlier post should to the trick.

Bob
laurentmartin 13 Oct, 2011
I never succeed to solve this.

Does anyone can help ?
GreyHead 15 Oct, 2011
Hi laurentmartin ,

What is the code that you have tried?

Bob
laurentmartin 23 Oct, 2011
Hi Bob,

Here is what I input as code I tried

For the Form :

<div class="form_item">
  <div class="form_element cf_radiobutton">
    <label class="cf_label" style="width: 150px;">Filling type</label>
    <div class="float_left">
      <input value="Choice 1" title="" class="radio" id="radio00" name="radio0" type="radio" />
      <label for="radio00" class="radio_label">Choice 1</label>
      <br />
      
<input value="Choice 2" title="" class="radio" id="radio01" name="radio0" type="radio" />
      <label for="radio01" class="radio_label">Choice 2</label>
      <br />
      

    </div>
    
  </div>
  <div class="cfclear"> </div>
</div>

<div class="form_item">
  <div class="form_element cf_button">
    <input value="Next" name="button_1" type="submit" />
  </div>
  <div class="cfclear"> </div>
</div>

For the onsubmit before e-mail
<?php
$radioname = JRequest::getString('radioname', 'http://www.google.de', 'post');
if ($radioname == 'Choice 1') {
  $url = 'http://www.google.com';
} else {
  $url = 'http://www.yahoo.com';
}
$MyForm->formrow->redirecturl = $url;
?>



Any suggestion ?
GreyHead 23 Oct, 2011
HI laurentmartin,

That looks good except that the name of the radio button is 'radio0' and not 'radioname'.

Bob
laurentmartin 23 Oct, 2011
i changed the name but it doesn't work better
GreyHead 23 Oct, 2011
Hi laurentmartin,

It works OK here. Do you have Send Emails set to Yes on the form General tab? If not the OnSubmit Before code will not be run.

Bob
laurentmartin 28 Oct, 2011
hi greyhead,

Indeed , need to activate the e-mail.

However, i still have a problem: it only redirects to Yahoo !!

Any idea what to correct ?
GreyHead 24 Nov, 2011
Hi laurentmartin ,

What code so you now have?

Bob
This topic is locked and no more replies can be posted.