You need to sign in to do that
Don't have an account?
Venkat Dodla
passing user input field data from VF page to controller using Remote Action
Hi Friends,
I have below code that statically passes value from VF Page to controller when a button is clicked. I am looking code to provide 2 input fields like name and phone no. and have user type the value into these field and when user clicks on submit button, the values should be sent to controller.
Thanks for your time and help.
APEX Controller:
Global class RemoteActionCall_Controller {
//Variable under test
public static String valuefromJS {get; set;}
@RemoteAction
global static void someMethod(String parameter){
valuefromJS = parameter;
System.debug(valuefromJS);
// return null;
}
}
VF Page for performing RemoteAction:
<apex:page id="myPage" controller="RemoteActionCall_Controller">
<!-- Include JQuery in the page -->
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<style type="text/css">
.pointer
{
cursor:pointer;
border:1px solid #ccc;
padding:5px;
}
</style>
<script type="text/javascript">
function sendVariable(parameter){
Visualforce.remoting.Manager.invokeAction(
'{!$RemoteAction.RemoteActionCall_Controller.someMethod}',
parameter,
function(result, event){
if (event.status) {
//do something here
console.log('Oh Ya');
}
},
{escape: true}
);
}
</script>
<apex:form >
<!-- Command button to set the value -->
<apex:commandButton value="Set Value" onclick="sendVariable('New Value'); return false;" />
</apex:form>
</apex:page>
I have below code that statically passes value from VF Page to controller when a button is clicked. I am looking code to provide 2 input fields like name and phone no. and have user type the value into these field and when user clicks on submit button, the values should be sent to controller.
Thanks for your time and help.
APEX Controller:
Global class RemoteActionCall_Controller {
//Variable under test
public static String valuefromJS {get; set;}
@RemoteAction
global static void someMethod(String parameter){
valuefromJS = parameter;
System.debug(valuefromJS);
// return null;
}
}
VF Page for performing RemoteAction:
<apex:page id="myPage" controller="RemoteActionCall_Controller">
<!-- Include JQuery in the page -->
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<style type="text/css">
.pointer
{
cursor:pointer;
border:1px solid #ccc;
padding:5px;
}
</style>
<script type="text/javascript">
function sendVariable(parameter){
Visualforce.remoting.Manager.invokeAction(
'{!$RemoteAction.RemoteActionCall_Controller.someMethod}',
parameter,
function(result, event){
if (event.status) {
//do something here
console.log('Oh Ya');
}
},
{escape: true}
);
}
</script>
<apex:form >
<!-- Command button to set the value -->
<apex:commandButton value="Set Value" onclick="sendVariable('New Value'); return false;" />
</apex:form>
</apex:page>
https://salesforce.stackexchange.com/questions/2114/passing-variables-from-controller-to-page-using-remoteaction
http://dazeworks.com/wtw/remote-action-action-function/
http://ramakavanan.blogspot.com/2015/07/javascript-remoting-pass-parameters.html
https://webkul.com/blog/use-apexparam-tag-visualforce/
https://gist.github.com/sravalaparthi/97e171a4025e6e271387
http://blog.ipointsystems.com/2017/march/connecting-javascript-with-apex
var t1 = <apex:inputText id="txt1" />
Visualforce.remoting.Manager.invokeAction(
'{!$RemoteAction.RemoteActionCall_Controller.someMethod}',
t1,
Kindly let me know if anyone can shed some ideas. thanks.
VisualForce Page using Javascript remoting:
<apex:page controller="RemoteActionCall_Controller">
<script type="text/javascript">
function sendVariable(){
var name = document.getElementById('txt1').value;
var address = document.getElementById('txt2').value;
var phone = document.getElementById('txt3').value;
if(name.length<1)
alert('Please enter name');
else{
RemoteActionCall_Controller.someMethod( name,address,phone,
function(result, event){
if (event.status) {
document.getElementById("{!$Component.pb.pbs.pbsi1.nameid}").innerHTML= result
document.getElementById("{!$Component.pb.pbs.pbsi2.addressid}").innerHTML= result
document.getElementById("{!$Component.pb.pbs.pbsi3.phoneid}").innerHTML= result
}
else if(result.type === 'exception')
alert("Exception caught");
else
alert("Exception caught");
}); }
}
</script>
Name:<input id="txt1" type ="text" /><br/>
Address:<input id="txt2" type="text" /> <br/>
Phone:<input id="txt3" type="text" /> <br/>
<button onclick="sendVariable()">Submit</button>
<apex:pageBlock id="pb">
<apex:pageBlockSection id="pbs">
<apex:pageBlockSectionitem id="pbsi1">
<apex:outputText id="nameid"/>
</apex:pageBlockSectionitem>
<apex:pageBlockSectionitem id="pbsi2">
<apex:outputText id="addressid"/>
</apex:pageBlockSectionitem>
<apex:pageBlockSectionitem id="pbsi3">
<apex:outputText id="phoneid"/>
</apex:pageBlockSectionitem>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:page>
APEX Handler/Controller:
/********************************************
created on 03/07/2019 by vdodlasf@gmail.com
*********************************************/
Global class RemoteActionCall_Controller {
public static string name {get;set;}
@RemoteAction
public static void someMethod(String name,String address, String phone){
System.debug('*********** Name ****************'+name);
System.debug('*********** address ****************'+address);
System.debug('*********** phone ****************'+phone);
}
}