1
votes

I am having a visualforce component with some script written in it and I directly want to pass some values to the controller.this is My javascript code.

<script>
function uploadComplete(evt) {
  var city = 'Shimla';
  var location = 'kkllkk'
  **i want to pass city and location in IWantToDebug method**
  function IWantToDebug() {

         MyController.IWantToDebug(city,location, function(result, event) {
    // Set the inputField value to the result in here
   });
} </script>

My apex controller class method is like.

 public void IWantToDebug(String city , String location) {

      System.debug('======================= ' + data);

 }
1

1 Answers

1
votes

You can use actionfunction to pass the values to controller

add following to your html part of component:

<apex:actionFunction action="{!IWantToDebug}" name="IWantToDebugJavascriptSide" rerender="someComponentIdToRender">
    <apex:param name="city " value=""/>
    <apex:param name="location" value=""/>
</apex:actionFunction>

change your javascript to something like

    <script>
       function uploadComplete(evt) {
          var city = 'Shimla';
          var location = 'kkllkk'
          **i want to pass city and location in IWantToDebug method**
          function IWantToDebug() {
          IWantToDebugJavascriptSide(city,location);
      });
   </script>

And change your Controller to something like

public PageReference IWantToDebug() {
     String city , String location;
     if (Apexpages.currentPage().getParameters().containsKey('city')){
       city = Apexpages.currentPage().getParameters().get('city'));
     }
     if (Apexpages.currentPage().getParameters().containsKey('location')){
        location= Apexpages.currentPage().getParameters().get('location'));
     }

     System.debug('======================= ' + city + ' ' +location);
     return null;
  }

For more reference on how to use actionfunction please visit https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_actionFunction.htm

Thank you