doujiyan0031 2015-06-18 13:50
浏览 80

从php服务中读取HTML中的变量

I am trying to build a website that displays a google map with a user location (lat/long) from a php service that I wrote.

I already have a php script that gets the lat/long from a mobile app (via POST from the client), stores it in a DB, and read it back from the DB into two variables, let's call them $lat and $long. To make sure I have the right values in $lat and $long, I did a simple echo and got the two values.

I am struggling with understanding how to read these values from my index.html script. All the examples that I have seen suggest keeping the php code in the html file but I would rather keep them separate. I am also not sure how to assign these values to parameters in HTML/Javacript so I can actually display them on the map.

So my questions are: 1. how do I call that php file from HTML? 2. and how do I read $lat and $long from the php service and assign them to parameters in HTML/Javascript that I can display on the map?

EDIT: here is my PHP code and my index.html (Which is a 1:1 copy from the Google Maps v3 docs).

location.php:

$content = file_get_contents('php://input');
$post_data = json_decode($content , true);
$lat = $post_data['lat'];
$long = $post_data['long'];

//CONNECT TO MYSQL
$con1 = mysql_connect("localhost", "francesco", "bbbbbb", "location111");
if ($con1->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$db_selected = mysql_select_db('location111');
if(mysql_select_db('location111')) { echo 'true    '; } else { echo 'falise'; } 
if (!$db_selected) {
    die ('Can\'t use foo : ' . mysql_error());
}

if (!empty($lat)) {
    $sql = "INSERT INTO LocationInfo (latitude, longitude) 
                              VALUES ('$lat', '$long');";
    mysql_query($sql) or die ('Error updating database: ' . mysql_error());
 }

$read_query = "SELECT * FROM LocationInfo;";
$results = mysql_query($read_query) or die ('Error reading from database: ' . mysql_error());
$column = array();

while($row = mysql_fetch_array($results)){
    $column[] = $row;
}


$current_lat = $column[sizeof($column) - 1]['latitude'];
$current_long = $column[sizeof($column) - 1]['longitude'];

echo $current_lat;
echo $current_long;

?>

index.html:

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title>My GeoLocation</title>
    <style>
      html, body, #map-canvas {
        height: 100%;
        margin: 0px;
        padding: 0px
      }
    </style>
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true"></script>
    <script>

var map;

function initialize() {
 // var myLatlng = new google.maps.LatLng(40.7127840,-74.0059410);
  var mapOptions = {
    zoom: 14
    //mapTypeId: google.maps.MapTypeId.ROADMAP
    //center: myLatlng
  };

  map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

  if(navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
      var pos = new google.maps.LatLng(position.coords.latitude,
                                       position.coords.longitude);

      var infowindow = new google.maps.InfoWindow({
        map: map,
        position: pos,
        content: 'Location found using HTML5.'
      });

      map.setCenter(pos);
    }, function() {
      handleNoGeolocation(true);
    });
  } else {
    // Browser doesn't support Geolocation
    handleNoGeolocation(false);
  }

}

function handleNoGeolocation(errorFlag) {
  if (errorFlag) {
    var content = 'Error: The Geolocation service failed.';
  } else {
    var content = 'Error: Your browser doesn\'t support geolocation.';
  }

  var options = {
    map: map,
    position: new google.maps.LatLng(60, 105),
    content: content
  };

  var infowindow = new google.maps.InfoWindow(options);
  map.setCenter(options.position);
}

google.maps.event.addDomListener(window, 'load', initialize);

    </script>
  </head>
  <body>
    <div id="map-canvas"></div>
  </body>
</html>
  • 写回答

1条回答 默认 最新

  • dousi6303 2015-06-18 13:57
    关注

    First of all, if you want to display something in template with php, it must have .php extension, not .html You can then use your php inside of inline javascript like that:

    <script type="text/javascript">
    
    var long = "<?php echo $long;?>";
    var lat= "<?php echo $lat;?>";
    
    </script>
    

    Or you can use Ajax on other hand

    评论

报告相同问题?

悬赏问题

  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)