Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8024077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:45:24+00:00 2026-06-04T22:45:24+00:00

I have coded this function to detect and update postLat and postLon variables. function

  • 0

I have coded this function to detect and update postLat and postLon variables.

function searchAddSubmit(){
    var shopId = 1; //random number to test the function
    var postLon, postLat;
    var shopTime = $("#selected-search-result-minutes").val();
    navigator.geolocation.getCurrentPosition(function(position){
        postLat=position.coords.latitude;
        postLon=position.coords.longitude;
        console.log(postLat);
        console.log(postLon);
        console.log('reun');
    },function(error){});
    console.log(postLat+" 1231");
    console.log(postLon+" 1231");
    $.ajax({
        type: "POST",
        url: "search-add-process.php",
        cache: false,
        dataType: "json",
        data:{
            id: shopId,
            time: shopTime, //this value is taken from a form input
            lat: postLat,
            lon: postLon
        },
        success: function (data) {
            if(data.error === true) {
                alert(data.msg);
            } else {
                alert(data.msg);
                //remove output when successful
            }
        }
    });
}

I used Chrome’s Javascript Console tool to examine the variables (as printed with console.log()) and this is the result:

undefined 1231
undefined 1235
1.2957797
103.8501069
ruen

Coming to this point, I have these questions:

  1. Why are the variables postLon and postLat are not updated? In fact, the undefined variables are passed over to search-add-process.php in the ajax block, not the actual values.
  2. Why navigator.geolocation.getCurrentPosition() function runs after the console.log(postLat+" 1231") line? Correct me if I am wrong, I always assumed that Javascript would execute line by line.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T22:45:25+00:00Added an answer on June 4, 2026 at 10:45 pm

    You first tell the Geolocation API to try to find the current position of the user, and you let it go and search while you do the rest of your work. This is called Asynchronous Execution. In other words, you let the Geolocation API take its time to search and find the place of the user, while you do the rest of your job (rest of your code is getting executed). Then when the API found the position, it executes the function you’ve passed to it.

    Take the code of ajax into the success callback and everything would be fine.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code: _trackit: function(){ for(var key in this.items.sublinks){ switch(key){ case 'shoes': for(var
I have code like this var MyObj = { f1 : function(o){ o.onmousedown =
I have code like this: function search_keyword(){ $keyword = $this->db->escape_like_str(trim($_POST['keyword'])); $sql = ( SELECT
I have code like this: function search_keyword(){ $keyword = trim($_POST['keyword']); $search_explode = explode( ,
I have code like this: function search_keyword(){ $keyword = trim($_POST['keyword']); $search_explode = explode( ,
I have this code: function submitTwice(f){ f.action = 'http://mydomain.com/threevideopage.php'; f.target='name'; f.submit(); } I left
this is function.php of a wordpress theme that NOD32 says this code have a
i have this code $(function() { $('#ans_vote a span').click(function(){alert('working');return false;});}); and this html <div
I have this code: $(div[id^='intCell']).mouseover(function() { $(this).css({ border:,1px solid #ff097c}); }).mouseout(function() { $(this).css({border:,1px solid
I have this code in a function $_SESSION['id'] = $id; $_SESSION['pineapple'] = $hashbrown; $_SESSION['username']

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.