简体   繁体   中英

Accessing a variable from js file

Wrote the code for the main site, the geolocation code and the geocoder code.

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0"
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Electromart</title>
    <!-- <link href="//db.onlinewebfonts.com/c/8274996f8cf973b15814827fa281e485?family=TechnojunkW00-Regular" rel="stylesheet" type="text/css"/>-->
    <link rel="stylesheet" href="css/eshop.css">
</head>
<body>
<header class="header">
    <div class="header__container"> 
        <div class="topheader">
            <div class="topheader1">
                <?php

                ?>
            </div>
        </div>  
        <div class="header__item">
            <div class="logoimage">
                <img src="images/logo3.jpg" width="100" height="75" id="idsettingsforlogo" ></img>
            </div>
        </div>
    </div>
<script type="text/javascript" src="js/geoloc.js"></script>
<script type="text/javascript" src="js/geocode.js"></script>
</header>

<footer class="footer">

</footer>
</body>
</html>

geocode.js

 var geocoder = new google.maps.Geocoder();
    geocoder.geocode({address: lat + ',' + lng}, function (results, status) {
        if (status !== google.maps.GeocoderStatus.OK || !results[0]) {
            return;
        }
        var result = results[0];

        var city, region, country;

        for (var i = 0; i < result.address_components.length; i++) {
            if (result.address_components[i].types[0] === "locality") {
                city = result.address_components[i];
            }
            if (result.address_components[i].types[0] === "administrative_area_level_1") {
                region = result.address_components[i];
            }
            if (result.address_components[i].types[0] === "country") {
                country = result.address_components[i];
            }
        }

        // alert(city.long_name + ", " + region.long_name + ", " + country.short_name)

        console.log(results);
    });

Tell me how you can access the city and country variables of the geocode.js file in the main file in the php block. Not sure how to use the Tags (), if you can do without them, write about it.

PS In the geocode function, the alert function was used to display the variables, with direct access to the city, country, region variables, and how to display these variables in main.html or main.php - how to access them?

An example you can use Cookie like(example you must edit):

$(document).ready(function () {
  createCookie("height", $(window).height(), "10");
});

function createCookie(name, value, days) {
  var expires;
  if (days) {
    var date = new Date();
    date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
    expires = "; expires=" + date.toGMTString();
  }
  else {
    expires = "";
  }
  document.cookie = escape(name) + "=" + escape(value) + expires + "; path=/";
}

And then read it with PHP:

<?PHP
   $_COOKIE["height"];
?>

Else you can use

js can set cookies and reload the page. And php from cookies will take data. (or can php define geo data, example: https://www.php.net/manual/en/book.geoip.php )

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