Passing JavaScript values to Apex class

Inside Page 


 <script>
    getLocation();
    function getLocation() {
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(showPosition);
        } else {
            // ....... else code .......
        }
    }
    function showPosition(position) {
          jsToVfAction(position.coords.latitude, position.coords.longitude);
    }
    </script>

   <apex:actionFunction name="jsToVfAction" action="{!copyInToApex}" rerender="apexValues">
            <apex:param name="one" value="" />
            <apex:param name="two" value="" />
        </apex:actionFunction>

<apex:outputPanel id="apexValues"> 
            <apex:outputText value="{!FirstValue}" /><br/>
            <apex:outputText value="{!SecondValue}" />
        </apex:outputPanel>


Inside Controller


public String FirstValue { get; set; }
    public String SecondValue { get; set; }

public PageReference copyInToApex() 
        {
        FirstValue = Apexpages.currentPage().getParameters().get('one');
        SecondValue = Apexpages.currentPage().getParameters().get('two');
        return null;
        }


Comments

Popular posts