+ Start a Discussion
ethan huntethan hunt 

Passing values from VF page to controller.

HI,

 

I am trying to fetch values from visual force page to controller. My requirement is  - 

 

1. I have 4 textbox in my vf page where i will enter 4 integer values.

2. Those values should be given to controller for doing average function and return back the result to 5th textbox in my vf page.

 

My VF page code - 

<apex:page controller="test1" id="pg">
<apex:form >
<apex:pageBlock id="frm">
<apex:pageBlockSection columns="1">
<apex:inputText label="Num1:" id="text1" />
<apex:inputText label="Num2:" id="text2"/>
<apex:inputText label="Num3:" id="text3"/>
<apex:inputText label="Num4:" id="text4"/>
<apex:commandButton value="Click" id="button" style="width:80px"/>
<apex:inputText label="Result:" id="result"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
<script type='text/javascript'>
function getvalue()
{
var a = document.getElementById('{!$Component.pg.frm.text1}').value
var b = document.getElementById('{!$Component.pg.frm.text2}').value
var c = document.getElementById('{!$Component.pg.frm.text3}').value
var d = document.getElementById('{!$Component.pg.frm.text4}').value
var e = document.getElementById('{!$Component.pg.frm.result}').value
}
</script>

</apex:page>

 

 

My Controller code - 

public with sharing class test1
{
public integer text1{get;set;}
public integer text2{get;set;}
public integer text3{get;set;}
public integer text4{get;set;}

public decimal avg(integer tex1,integer tex2,integer tex3,integer tex4)
{

integer result;
result = ((text1+text1+text1+text1)/4);
return result;
}}

 

 

. Kindly provide me solution how to achieve this scneario.

 

Thanks in Advance

Sidhartha

Best Answer chosen by Admin (Salesforce Developers) 
Amritesh SinghAmritesh Singh

Simple Solution :

 

VF PAGE  :

 

<apex:page controller="getAvgclass">

 <apex:form >
<apex:pageBlock id="frm">
<apex:pageBlockSection columns="1">
<apex:inputText label="Num1:" id="text1" value="{!text1}"/>
<apex:inputText label="Num2:" id="text2" value="{!text2}"/>
<apex:inputText label="Num3:" id="text3" value="{!text3}"/>
<apex:inputText label="Num4:" id="text4" value="{!text4}"/>
</apex:pageBlockSection>

</apex:pageBlock>

<div style="position:relative;left:210px;">
<apex:commandButton value="Click" id="button" style="width:80px" action="{!doAvg}" reRender="displayAvg" />
</div>
<apex:pageBlock id="displayAvg" >
<apex:outputPanel rendered="{!show}">
Average Result :{!result}
</apex:outputPanel>
</apex:pageBlock>
</apex:form>
</apex:page>

 

 

CONTROLLER :

 

public with sharing class getAvgclass{

public boolean show { get; set; }

public decimal result { get; set; }
public decimal text1{get;set;}
public decimal text2{get;set;}
public decimal text3{get;set;}
public decimal text4{get;set;}

public getAvgclass() {
show =false;
}

public PageReference doAvg() {
show =true;
result = ((text1+text2+text3+text4)/4);
return null;
}
}

 

All Answers

thatheraherethatherahere

Hi,

For this Scenario I will suggest you Javascript Remoting for Apex Controllers.
Visit - http://www.salesforce.com/us/developer/docs/pages/Content/pages_js_remoting.htm and learn about it.

 

Govind Thathera.

Satish_SFDCSatish_SFDC
Also, please look into the <apex:param> vf tag. You can use the assignTo attribute to pass values to some variables in your controller.

Regards,
Satish Kumar

Please mark my answer as a solution if it was helpful so it is available to others as a proper solution.
If you felt I went above and beyond, please give me Kudos by clicking on the star icon.
Amritesh SinghAmritesh Singh

Simple Solution :

 

VF PAGE  :

 

<apex:page controller="getAvgclass">

 <apex:form >
<apex:pageBlock id="frm">
<apex:pageBlockSection columns="1">
<apex:inputText label="Num1:" id="text1" value="{!text1}"/>
<apex:inputText label="Num2:" id="text2" value="{!text2}"/>
<apex:inputText label="Num3:" id="text3" value="{!text3}"/>
<apex:inputText label="Num4:" id="text4" value="{!text4}"/>
</apex:pageBlockSection>

</apex:pageBlock>

<div style="position:relative;left:210px;">
<apex:commandButton value="Click" id="button" style="width:80px" action="{!doAvg}" reRender="displayAvg" />
</div>
<apex:pageBlock id="displayAvg" >
<apex:outputPanel rendered="{!show}">
Average Result :{!result}
</apex:outputPanel>
</apex:pageBlock>
</apex:form>
</apex:page>

 

 

CONTROLLER :

 

public with sharing class getAvgclass{

public boolean show { get; set; }

public decimal result { get; set; }
public decimal text1{get;set;}
public decimal text2{get;set;}
public decimal text3{get;set;}
public decimal text4{get;set;}

public getAvgclass() {
show =false;
}

public PageReference doAvg() {
show =true;
result = ((text1+text2+text3+text4)/4);
return null;
}
}

 

This was selected as the best answer