+ Start a Discussion
Shohrat MuhamovShohrat Muhamov 

Pass value from JavaScript to Controller

Hello,
I need to pass user input from javascript in visualforce button to Apex controller, to add it to field. Can anyone hep me with it please?
Raj VakatiRaj Vakati
Refer this link 

https://ohotech.com/2015/04/pass-value-of-variables-from-visualforce-to-controller-via-javascript.html
https://www.jitendrazaa.com/blog/salesforce/passing-parameter-in-actionfunction-in-visualforce/


For additional info 


https://gist.github.com/sravalaparthi/97e171a4025e6e271387
https://developer.salesforce.com/blogs/developer-relations/2009/10/passing-javascript-values-to-apex-controller.html
Deepali KulshresthaDeepali Kulshrestha
Hi Shohrat,

You can pass the value from javascript to controler by action function:

Please have a look at this code:

<apex:page showHeader="true" sidebar="true" controller="SampleController">
    <script type="text/javascript">
           window.onload = function(){
               //this is calling the apex:actionFunction below with the same name when the page loads
               setUserLocation('11,13');
               console.log('setUserLocation called');
           }
   </script>

    <apex:form >
        <!-- this action function can be called from javascript -->
        <apex:actionFunction name="setUserLocation" action="{!setUserLocationInApex}" rerender="">
            <apex:param name="userLocation" value="" />
        </apex:actionFunction>
    </apex:form>
For more infomation you can also refer to this link:-
https://salesforce.stackexchange.com/questions/24666/how-to-pass-javascript-value-to-controller

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
 
Shohrat MuhamovShohrat Muhamov
Hello Deepali,

I have tried your method before, but it didn't work for me. I am using promt  function from java script to get user input.

Best Regards,
Shohrat Muhamov
Ajay K DubediAjay K Dubedi
Hi Shohrat,
You can use remoting action to pass data from javascript to Apex controller. Please try the below code and let me know if this works for you.
Apax Page:
<apex:page controller="RemoteControllerExample">
    <apex:slds />
    <div class="slds-grid slds-gutters">
        <div class="slds-col">
            <span>  <input type="text" name="name" class="slds-input" id="name"/></span>
        </div>
        <div class="slds-col">
            <span><input type="text" name="billingAdd" class="slds-input" id="billingAdd"/></span>
        </div>
        <div class="slds-col">
            <span><button class="slds-button slds-button_brand" onclick="fun();">Save</button></span>
        </div>
    </div>
    <script>
    function fun() {
        var name = document.getElementById("name").value;
        var billing_add = document.getElementById("billingAdd").value;
        RemoteControllerExample.RemoteControllerExampleMethod(name,billing_add,function(result,event){
            if(event.status)
            {console.log(result);}
        });
        
    }
    </script>
</apex:page>

Controller :

public class RemoteControllerExample {
    @RemoteAction
    public static Account RemoteControllerExampleMethod(String name,String billing_city) {
        Account accObj = new Account();
        accObj.Name = name;
        accObj.BillingCity = billing_city;
        insert accObj;
        return accObj;
    }
}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi