0

I want to get the location from this JavaScript to the controller and store it in the database:

var currPosition;

navigator.geolocation.getCurrentPosition(function(position) {
  updatePosition(position);
  setInterval(function() {
    var lat = currPosition.coords.latitude;
    var lng = currPosition.coords.longitude;
    jQuery.ajax({
      type: "POST",
      url: "myURL/location.php",
      data: 'x=' + lat + '&y=' + lng,
      cache: false
    });
  }, 1000);
}, errorCallback);

var watchID = navigator.geolocation.watchPosition(function(position) {
  updatePosition(position);
});

function updatePosition(position) {
  currPosition = position;
}

function errorCallback(error) {
  var msg = "Can't get your location. Error = ";
  if (error.code == 1)
    msg += "PERMISSION_DENIED";
  else if (error.code == 2)
    msg += "POSITION_UNAVAILABLE";
  else if (error.code == 3)
    msg += "TIMEOUT";
  msg += ", msg = " + error.message;
  alert(msg);
}
2
  • 1
    You are getting any error? Whats your question? Commented Sep 25, 2019 at 13:19
  • iam new to codeigniter framework please can you tell me how to post those watch-position coordinates from java script to controller Commented Sep 25, 2019 at 13:23

2 Answers 2

1

To send post with .ajax():

// ....
let formData = new FormData();
const lat = currPosition.coords.latitude;
const lng = currPosition.coords.longitude;
formData.append("x", lat);
formData.append("y", y lng;
$.ajax({
    url: "myURL/location.php", // update this with you url
    dataType: 'text',
    cache: false,
    contentType: false,
    processData: false,
    data: formData,
    type: 'POST',
    success: function(data){
        const response = jQuery.parseJSON(data);
    }
});
//....

To receive post data in codeigniter :

$x = $this->input->post('x');
$y = $this->input->post('y');
Sign up to request clarification or add additional context in comments.

3 Comments

i still get null values " INSERT INTO location (x, y) VALUES (NULL, NULL)" Uncaught ReferenceError: base_url is not defined
@KishanChandra may be you not declare class extend CI_Controller.. See this case: stackoverflow.com/questions/53944667/…
hi, thanks i managed to solve it just by adding <script>var base_url = '<?php echo base_url() ?>';</script>
1

you just need to change uri parameter to codeginter route that you have

setInterval(function(){
    ....
    url:  "change this to codeigniter route url", 
    ....
}, 1000);

then in the controller you just need to save those parameter,

class X extends CI_Controller{

    function update_position(){
        $x = $this->input->post('x');
        $y = $this->input->post('y');

        // then save it using model or query.
        $this->model_name->insert([...])
    }
}

2 Comments

i still get null values " INSERT INTO location (x, y) VALUES (NULL, NULL)"
Uncaught ReferenceError: base_url is not defined

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.