【问题标题】:Passing jQuery variables to external PHP file将 jQuery 变量传递给外部 PHP 文件
【发布时间】:2019-07-09 21:42:06
【问题描述】:

对于我的 Web 应用程序,我正在尝试将 javascript (googleMap.js) 中的纬度和经度变量发布到外部 php 文件 (closestLocations.php)。我正在使用 AJAX 发布方法来发布两个变量,但是我仍然发现这些变量是未定义的。

closestLocations.php 文件正在生成一个 xml 文件,其中包含与特定位置相关的位置列表(目前纬度和经度是硬编码的,但我想将其更改为这些变量)。

任何帮助将不胜感激

googleMap.js

    if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
        latitude = position.coords.latitude;
        longitude = position.coords.longitude;
        myPosition = {lat: latitude, lng: longitude};

        $.ajax({
            type: "POST",
            data: {lat:latitude,lng:longitude},
            url: "php/closestLocations.php",
            success: function(data){
                console.log(latitude);
            }
        });

        //personalise infowindow 
        infoWindow.setPosition(myPosition);
        infoWindow.setContent('You Are Here!');
        infoWindow.open(map);
        map.setCenter(myPosition);

        //display marker on map where user is located
        var marker = new google.maps.Marker({
            map: map,
            position: myPosition,
        });

    }, function() {
        handleLocationError(true, infoWindow, map.getCenter());
    });
} else {
    handleLocationError(false, infoWindow, map.getCenter());
}

closestLocations.php

require_once("closestLocations.php");

function parseToXML($htmlStr)
{
    $xmlStr=str_replace('<','&lt;',$htmlStr);
    $xmlStr=str_replace('>','&gt;',$xmlStr);
    $xmlStr=str_replace('"','&quot;',$xmlStr);
    $xmlStr=str_replace("'",'&#39;',$xmlStr);
    $xmlStr=str_replace("&",'&amp;',$xmlStr);
    return $xmlStr;
}

$connection=mysqli_connect ('localhost', $username, $password);
if (!$connection) {
  die('Not connected : ' . mysqli_error());
}

// Set the active MySQL database
$db_selected = mysqli_select_db($connection, $database);
if (!$db_selected) {
  die ('Can\'t use db : ' . mysqli_error());
}

$query = "SELECT locationName, address, town, postcode, telephone, ( 3959 * acos ( cos ( radians(50.6346) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-6.6169) ) + sin ( radians(50.6346) ) * sin( radians( lat ) ) ) ) AS distance FROM defibrillators HAVING distance < 5 ORDER BY distance LIMIT 0 , 10 ";
$result=mysqli_query($connection, $query);
if(!$result){
    die('Invalid query: ' . mysqli_error());
} 

header("Content-type: text/xml");

// Start XML file, echo parent node
echo "<?xml version='1.0' ?>";
echo '<markers>';
$ind=0;
// Iterate through the rows, printing XML nodes for each
while ($row = @mysqli_fetch_assoc($result)){
  // Add to XML document node
  echo '<marker ';
  echo 'name="' . parseToXML($row['locationName']) . '" ';
  echo 'address="' . parseToXML($row['address']) . '" ';
  echo 'town="' . parseToXML($row['town']) . '" ';
  echo 'postcode="' . parseToXML($row['postcode']) . '" ';
  echo 'telephone="' . parseToXML($row['telephone']) . '" ';
  echo '/>';
  $ind = $ind + 1;
}

// End XML file
echo '</markers>';

【问题讨论】:

标签: javascript php jquery ajax google-geolocation


【解决方案1】:

您的 JS ajax 调用正在通过 POST 方法请求 PHP 文件,因此您在数据中发布的变量将在 $_POST 超全局的 PHP 脚本中可用。

var_dump($_POST); // array(2) { ["lat"]=> string(1) "1" ["lng"]=> string(1) "2" }

所以纬度可以通过 $_POST['lat'];和经度通过 $_POST['lng']

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-11
    • 2013-02-19
    • 2020-06-06
    相关资源
    最近更新 更多