简体   繁体   中英

Storing Location coordinates in Database(mysql)

Hello Guys I am facing Problem in Getting The User location coordinates in my PHP code to Insert it into my Database.....

I have tried a lot of ways,Using DOM and other but no one worked.... I was Able to Print coordinates or JSON.stringify returned String on Browser but couldn't Updated it in database..... and the following PHP Script return an Error is... ERROR::::Notice: Undefined index: coords in C:\xampp\htdocs\PHP\PRoject\php\Show.php on line 69 NULL

Please Help,my Code is below

//PHP

$coords=$_POST['coords'];

$decode= json_decode($coords,true);
var_dump($decode);


$query="UPDATE `coords` SET `coords`='$decode' WHERE 
        `driver`.`id`='$uid'";
mysqli_query($conn,$query);

//Javascript

  function initMap() {
    var x=navigator.geolocation;
  x.getCurrentPosition(success,failure);
  function success(pos){

  var y=pos.coords.latitude;
  var z=pos.coords.longitude;
    var coords= {lat:y,lng:z};

  map = new google.maps.Map(document.getElementById('map'), {
      center: coords,
      zoom: 8

    });
    var marker= new google.maps.Marker({
      position:coords,
      map:map,
    title:'my pos'
    });
    console.log(coords);
    var str=JSON.stringify(coords);
    console.log(str);
    $.ajax({
      type:'POST',
        url:'Show.php',
        data:{coords:str},
        success:function(str){
            alert(str);
        }
    });

  }

The problem was I was sending the post request to the same page means my php and js scripts were in same page. So I then send that post data to another page and it worked. Cheers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM