【问题标题】:I'm trying to pass a Javascript variable to PHP within the same file (Google Maps API) [duplicate]我正在尝试在同一文件(Google Maps API)中将 Javascript 变量传递给 PHP [重复]
【发布时间】:2016-07-20 15:53:21
【问题描述】:

我正在尝试建立一个小型网站来跟踪汽车的位置,并将数据存储在数据库中。目前,我已经使用 .php 文件进行了所有设置,其中包含 Javascript - 我真的不确定这是否可取,我对网络编程很陌生。

我需要将大脚本中的“lati”变量发送到 PHP 脚本(代码底部)。

代码一切正常,它跟踪用户并将数据提交到数据库,但它在数据库中填充的字段是空白的 :( 请参阅我的代码 -

<!DOCTYPE html>
<?php



$username = "ishuttle";
$password = "m7elH07yO2";
$hostname = "localhost"; 
$dbname = "ishuttle_taxi-location";

//connection to the database
$conn = mysqli_connect($hostname, $username, $password, $dbname) 
  or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";


 
?>
<html>
<head>
<title>Geolocation</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<style>
html, body {
	height: 100%;
	margin: 0;
	padding: 100;
}
#map {
	height: 70%;
	width: 100%;
}
</style>
</head>
<body>
<center>
  <h1>DANKCATT DRIVER SIDE APP</h1>
</center>
<div id="map"></div>
<div id="capture"></div>
<script>
      // Note: This example requires that you consent to location sharing when
      // prompted by your browser. If you see the error "The Geolocation service
      // failed.", it means you probably did not give permission for the browser to
      // locate you.

	var Lati;
	var Longi;    
	Lati = 3;
	Longi = 3;
 	function initMap() {
        var map = new google.maps.Map(document.getElementById('map'), {
          center: {lat: -34.397, lng: 150.644},
					  zoom: 15
        });
	var timerID = setInterval(function() {	  
        var infoWindow = new google.maps.InfoWindow({map: map});
	
        // Try HTML5 geolocation.
        if (navigator.geolocation) {
			
          navigator.geolocation.getCurrentPosition(function(position) {
            var pos = {
              lat: position.coords.latitude,
              lng: position.coords.longitude
            };
			var lati = {
              lat: position.coords.latitude
            };
			var longi = {
              lng: position.coords.longitude
            };
			console.log(lati, "Lol");
            infoWindow.setPosition(pos);
            infoWindow.setContent('lati');
            map.setCenter(pos);
          }, function() {
            handleLocationError(true, infoWindow, map.getCenter());
		
          });
        } else {
          // Browser doesn't support Geolocation
          handleLocationError(false, infoWindow, map.getCenter());
        }
    
      function handleLocationError(browserHasGeolocation, infoWindow, pos) {
        infoWindow.setPosition(pos);
        infoWindow.setContent(browserHasGeolocation ?
                              'Error: The Geolocation service failed.' :
                              'Error: Your browser doesn\'t support geolocation.');
      }
		
	  //in a loop (setInterval) get coords and apply them to database

	  
	  }, 10 * 1000); 
	  
	  }
	  //reverse this for the home page - drag the coords out of the db and display them on a loop
	 google.maps.event.addDomListener(window, 'load', getLocation); 
	  
    </script> 
<script async defer
        src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDONVV6mCAgATiPAanIbdfY55_felLGHHk&callback=initMap">
    </script>
<?php 
 
    
    $lati = $_GET['Lati'];
	$sql = "UPDATE driver_location SET Latitude = '$lati' WHERE ID_No = 1111;";
 echo "$lati";
  if ($conn->query($sql) === TRUE) {
    echo "Table MyGuests created successfully";
} else {
    echo "Error creating table: " . $conn->error;
}



?>
</body>
</html>

【问题讨论】:

  • 在插入数据库之前,检查你的变量是否获取到数据?
  • 已排序!我使用了 cookie 文档方法。非常感谢您将其标记为骗子...永远不会发现。

标签: javascript php google-maps-api-3


【解决方案1】:

您可能希望将您的 php 数据库代码分离到另一个文件中,以便您可以将 ajax 发布从您的 javascript 文件到 php 文件。

这是一个使用 jquery 的 ajax 帖子示例。这是允许您从 javascript 发布到 php 页面的 javascript:

 $.ajax({
        method: 'POST',
        url: 'likeComment.php',
        data: {'commentId': commentId},
        success: afterLikeComment,
        dataType: 'json'});

Learn Jquery是一个学习jquery的站点,这里是jquery站点本身Jquery site。 jQuery 是一个 Javascript 包装器,它使选择 dom 元素和发布到 php 等操作变得更加容易。

【讨论】:

  • 我最终将数据插入到 cookie 文档中,然后我用 PHP 从 cookie 中获取它们 :) 我描述得不是很好,但是见鬼,你比我更清楚反正。我会查看 Jquery,当然,我只需要将它添加到我正在尝试学习的网络垃圾列表中哈哈
猜你喜欢
  • 2016-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-06
  • 2014-01-11
  • 2016-02-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多