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
Ashu sharma 38Ashu sharma 38 

How to use radio button as DataType

Hello All,

Kindly give the suggestions on my scenrio:

I am creating a Custom object (Registration form) here i am make Vfpage also and i want to save the data into the database via VF Page;
Here i am using apex class and VF page(IT WORKS FINE)
Now i want to use some Radio button on it like Gender==Male and Female in Radio Button,so on this i have to create the datatype???
As i have to save the records in database through VF page entry.

public class BuilderSaveClass {
    
    public string name                         {set;get;}
    public string location                    {set;get;}
    public string state                     {set;get;}
    public boolean ActiveStatus             {Set;get;}
   // public list<SelectOption> country       {set;get;}  
    
    public void saveMe(){
       // country=new list<selectOption>();
        //country.add(new SelectOption('India','India'));
          //  country.add(new SelectOption('UK','UK'));
        Builder__c     bc=new Builder__c();
        bc.Name=name;
        bc.Location__c=location;
        bc.State__c=state;
        bc.Active__c=ActiveStatus;
            insert bc;
    }
    
    
    public void cancelMe(){
        name=null;
        location=null;
        state=null;
        ActiveStatus=null;
        
        
    }
}

VF PAGE
<apex:page controller="BuilderSaveClass">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockButtons location="top" >
                <apex:commandButton value="saveMe" action="{!saveMe}"/>
                <apex:commandButton value="Cancel" action="{!cancelMe}"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection columns="1" id="id">
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Name"/>
                    <apex:inputText value="{!name}"/>   
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Location"/>
                    <apex:inputText value="{!location}"/>
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                    State:<apex:selectList size="1" value="{!state}">
                    <apex:selectOption itemLabel="None" itemValue="None"></apex:selectOption>
                    <apex:selectOption itemLabel="Delhi" itemValue="DElhi"></apex:selectOption>
                    <apex:selectOption itemLabel="UP" itemValue="UP"></apex:selectOption>
                    <apex:selectOption itemLabel="PUNJAB" itemValue="PUNJAB"></apex:selectOption>
                    </apex:selectList>
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem>
                    ActiveStatus:<apex:inputCheckbox value="{!ActiveStatus}"/>
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
        </apex:pageBlock>
        
    </apex:form>
</apex:page>
SHAHNA MULLASHAHNA MULLA
MUHAMMED SEMIN P NMUHAMMED SEMIN P N
Hi nitish,
We can do this by using apex:inputHidden tag and a small piece of JavaScript.Here we will use onclick javascript to pass value to hidden variable. To show radio button on visualforce page use HTML tag. Pass the id of HTML radio button tag and apex:inputHidden tag to javascript. In javascript assign the value of HTML tag to apex:inputHidden.
Please refer below code...

Visualforce Page:
<apex:page controller="Radio">
    <apex:form >
        <input type="radio" name="radioName" value="Radio1" onclick="assignValue(this,'{!$component.hidValId}')"/> Radio 1 value
        <input type="radio" name="radioName" value="Radio2" onclick="assignValue(this,'{!$component.hidValId}')"/> Radio 2 value
        <input type="radio" name="radioName" value="Radio3" onclick="assignValue(this,'{!$component.hidValId}')"/> Radio 3 value
        <apex:inputHidden value="{!radioHidden}" id="hidValId" />
    </apex:form>
    <script>
        function assignValue(input, hidden){
          
            document.getElementById(hidden).value = input.value;
            alert(document.getElementById(hidden).value);
        }
    </script>
</apex:page>
Apex Class:
public class Radio{
    public string radioHidden {get;set;}
}
if it solve your problem please mark it as a best answer!!!
thanks,