简体   繁体   中英

How to post the latitude and longitude of my location in php using html?

I want to post the value of name,latitude,longitude and the phone number in php.I am using html as front end to collect the details.I am unable to post the latitude and longitude in php but i can post the name,phone number.How to post the latitude and longitude of my location in php?

<html>
<body>  

<form method="post" action="try2.php">
 First name:<input type="text" name="first_name" ><br>
 Phone:<input type="tel" name="phone" >

 <p><button onclick="geoFindMe()">Show my location</button></p>
 <div id="out"></div>
 <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;

    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>
   <input type="submit" value="submit" required>
  </form>

  Php code:
   <?php
    $first_name=$_POST["first_name"];
    $longitude=$_POST["longitude"];
    $latitude=$_POST["latitude"]
    $phone=$_POST["phone"];
    echo "$latitude";
    ?>

What I have mentioned is below, Now the value are set to the form field and can be posted to the script.

<html>
<body>  

<form method="post" action="try2.php">
 First name:<input type="text" name="first_name" ><br>
 Phone:<input type="tel" name="phone" >

 <p><button onclick="geoFindMe()">Show my location</button></p>
 <div id="out"></div>
 <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;
    document.getElementById("lat").value = latitude;
    document.getElementById("long").value = 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>
   <input id="lat" name="latitude" type="hidden" value=" ">
   <input id="long" name="longitude" type="hidden" value=" ">
   <input type="submit" value="submit" required>
  </form>

  Php code:
   <?php
    $first_name=$_POST["first_name"];
    $longitude=$_POST["longitude"];
    $latitude=$_POST["latitude"]
    $phone=$_POST["phone"];
    echo "$latitude";
    ?>

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