function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
khushi ahujakhushi ahuja 

Issue : Two radio buttons redirecting to same vf page

Hi, 
I want to develop a page which has two radio button as options : radio1  and radio2 . on click of radio1 a form of account should be avaialble and on click of radio2 a form of contact should be available. 

I have my code but on click of both radio button it is redirecting to contact page only. can u help me to solve my issue

<apex:page controller="AccController">
 <apex:form >
 <apex:pageBlock id="pgblk"> 
 <apex:selectRadio id="radioid" layout="pageDirection">
 <apex:selectOption  itemLabel="radio1" itemValue="1"></apex:selectOption>
 <apex:actionSupport event="onclick" action="{!AccForm}">
 </apex:actionSupport>
 
 <apex:selectOption itemLabel="radio2" itemValue="2"></apex:selectOption>
 <apex:actionSupport event="onclick" action="{!ContForm}">
 </apex:actionSupport>
 </apex:selectRadio>
 
 </apex:pageBlock>
 </apex:form>
</apex:page>

Here is my controller
public class AccController 
{
 public PageReference AccForm()
 {
 PageReference Page = new PageReference('/apex/CreateAcc');
 Page.setRedirect(true);
 return Page;
 }
 
 public PageReference ContForm()
 {
 PageReference Page = new PageReference('/apex/CreateContact');
 Page.setRedirect(true);
 return Page;
 }
}
Best Answer chosen by khushi ahuja
GauravendraGauravendra
Hi khushi,

Find the updated code: 
Vf Page:
<apex:page controller="AccController">
 <apex:form >
 <apex:pageBlock id="pgblk"> 
 <apex:selectRadio id="radioid" layout="pageDirection" value="{!selected}">
 <apex:selectOption itemLabel="radio1" itemValue="accountform"></apex:selectOption>
     <apex:actionSupport event="onclick" action="{!gotop}" />
 <apex:selectOption itemLabel="radio2" itemValue="contactform"></apex:selectOption>
     <apex:actionSupport event="onclick" action="{!gotop}" />

 </apex:selectRadio>
 </apex:pageBlock>
 </apex:form>
</apex:page>
AccController:
public class AccController 
{
    public String selected{get;set;}
    public void setSelected(String sel) {
        this.selected = sel;
    }
    public PageReference gotop() {
        PageReference page;
        System.debug('selected ##'+selected);
        if(selected =='accountform') {
            page = new PageReference('/apex/CreateAcc');
        }
        else if(selected =='contactform') {
            page = new PageReference('/apex/CreateContact');
        }

        return page;
    }
    
}
Make sure both the vf page exists i.e, CreateAcc and CreateContact.
You don't need to call two different function, one will do.

Hope this helps.

All Answers

GauravendraGauravendra
Hi khushi,

Find the updated code: 
Vf Page:
<apex:page controller="AccController">
 <apex:form >
 <apex:pageBlock id="pgblk"> 
 <apex:selectRadio id="radioid" layout="pageDirection" value="{!selected}">
 <apex:selectOption itemLabel="radio1" itemValue="accountform"></apex:selectOption>
     <apex:actionSupport event="onclick" action="{!gotop}" />
 <apex:selectOption itemLabel="radio2" itemValue="contactform"></apex:selectOption>
     <apex:actionSupport event="onclick" action="{!gotop}" />

 </apex:selectRadio>
 </apex:pageBlock>
 </apex:form>
</apex:page>
AccController:
public class AccController 
{
    public String selected{get;set;}
    public void setSelected(String sel) {
        this.selected = sel;
    }
    public PageReference gotop() {
        PageReference page;
        System.debug('selected ##'+selected);
        if(selected =='accountform') {
            page = new PageReference('/apex/CreateAcc');
        }
        else if(selected =='contactform') {
            page = new PageReference('/apex/CreateContact');
        }

        return page;
    }
    
}
Make sure both the vf page exists i.e, CreateAcc and CreateContact.
You don't need to call two different function, one will do.

Hope this helps.
This was selected as the best answer
SandhyaSandhya (Salesforce Developers) 
Hi,

Change your code to below.
 
<apex:page controller="AccController">
 <apex:form >
 <apex:pageBlock id="pgblk"> 
 <apex:selectRadio id="radioid" layout="pageDirection">
 <apex:selectOption itemLabel="radio1" itemValue="1">
  </apex:selectOption>
 <apex:actionSupport event="onclick" action="{!ContForm}">
 </apex:actionSupport>
   </apex:selectRadio>
 <apex:selectRadio id="radioid1" layout="pageDirection">
 <apex:selectOption itemLabel="radio2" itemValue="2">
 </apex:selectOption>
 <apex:actionSupport event="onclick" action="{!AccForm}">
 </apex:actionSupport>
 </apex:selectRadio>
 </apex:pageBlock>
 </apex:form>
</apex:page>

If this helps you, please mark it as solved so that it will be availabe for others as a solution.

Best Regards,
Sandhya