function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
nikita dhamalnikita dhamal 

Pass javascript values to apex controller

I have  a javascript on a vf page from which i want to pass value to the controller. but i am getting null in the controller.can anyone help me in this? my code:
VF page:

<apex:page controller="visitController" showHeader="false" sidebar="false" setup="true" standardStylesheets="false">
<apex:form > <style> body { padding: 20px; background-color:#ffffc9 } p { margin : 0; } </style>
<Script> function geoFindMe()
{ var output = document.getElementById("out");
if (!navigator.geolocation){
output.innerHTML = "<p>Geolocation is not supported by your browser</p>"; return;
}
function success(position)
{ var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
echo(latitude,longitude );
output.innerHTML = '<p>Latitude is ' + latitude + '° <br>Longitude is ' + longitude + '°</p>'; var img = new Image();
img.src = "https://maps.googleapis.com/maps/api/staticmap?center=" + latitude + "," + longitude + "&zoom=13&size=300x300&sensor=false"; output.appendChild(img); };
function error() {
output.innerHTML = "Unable to retrieve your location"; };
output.innerHTML = "<p>Locating…</p>";
navigator.geolocation.getCurrentPosition(success, error); }
</script>
<body>
 <apex:pageBlock id="block"> Sales Visit Name: <br />
<apex:inputField value="{!visit.name}" /><br />
Sales Visit Description: <br /> <apex:inputField value="{!visit.Description__c}" /><br /> Longitude: <br /> <apex:inputField value="{!visit.Longitude__c}" id="longitude" /><br />
Latitude: <br /> <apex:inputField value="{!visit.Latitude__c}" id="latitude" /><br /> <br/> <br/>
<apex:commandButton onclick="geoFindMe()" action="{!save}" value="Show my location!" /> <div id="out"></div>
<apex:actionFunction name="echo" action="{!echoVal}" reRender="resultPanel" status="myStatus">
<apex:param name="latitude" assignTo="{!enteredText1}" value="" />
<apex:param name="longitude" assignTo="{!enteredText2}" value="" />
</apex:actionFunction> </apex:pageBlock>
</body>
</apex:form>
</apex:page>

Controller:
public class visitController { 
    public String save { get; set; }

  public String Latval ;
  public String Lonval ;
    public Sales_Visit__c visit {get;set;}
 
 public String latitude {get;set;} 
 public String longitude{get;set;} 
  
    public visitController() {
        visit = new Sales_Visit__c();
        
       
    }   
 public void echoVal() 
    { 
    //  Latval = Apexpages.currentPage().getParameters().get('latitude');
     // Lonval = Apexpages.currentPage().getParameters().get('longitude'); 
     
    //  System.debug('*********latitude echoValechoVal latitude*********'+Latval);
     //      System.debug('*********longitude echoValechoVal longitude*********'+Lonval);
    } 

    
    public PageReference save() {
     Latval = Apexpages.currentPage().getParameters().get('latitude');
   Lonval = Apexpages.currentPage().getParameters().get('longitude'); 
     
     System.debug('*********latitudelatitude*********'+Latval);
           System.debug('*********longitudelongitude*********'+Lonval);
    
        insert visit;
        
        visit = new Sales_Visit__c();
        return null;
    }
}
VineetKumarVineetKumar
I guess you need to update the VF page code with the below tags :
<apex:param name="latitude" assignTo="{!latitude}" value="" />
<apex:param name="longitude" assignTo="{!longitude}" value="" />
You have not specified the enteredText1 and enteredText2 variables that might be causing the issue.
nikita dhamalnikita dhamal
Hi Vineet,
               As per notified by you iupdates the code still the value is null..
 
VineetKumarVineetKumar
Can you check your values beforce sending :
echo(latitude,longitude );
Jasveer SinghJasveer Singh
Hi Nikita dhamal

Use Visualforce.remoting.Manager.invokeAction function in javascript
for more details search on google like above line 

if helpful for you
mark solved 

Thanks & Regards
Jasveer Singh