简体   繁体   English

如何存储在php变量中的函数中找到的javascript变量值

[英]how to store javascript variable value found in a function in php variable

I'm trying to store the user's current latitude and longitude info as a PHP variable $lat . 我试图将用户当前的纬度和经度信息存储为PHP变量$lat Since it's in a function userLocated the value is not obtained. 由于它位于userLocated函数中,因此无法获取该值。 Here's the code: 这是代码:

<script type="text/javascript">

        navigator.geolocation.getCurrentPosition(userLocated);

        function userLocated(position){
        var latitude = position.coords.latitude;
        var longitude = position.coords.longitude;
        // document.write("latitude: " + latitude +" longitude " + longitude);

    }

</script>

<?php 

$lat ="<script> 
document.writeln(latitude); 
</script>"; 
echo "$lat";
?>

That is not how JavaScript and PHP interact with each other. 这不是JavaScript和PHP如何相互作用的方式。

PHP runs on the server and does it's job long before the user actually sees the website. PHP在服务器上运行,并且在用户实际看到网站之前很久就完成了它的工作。

JavaScript runs on the browser (in this case anyway) and does it's job long after PHP has done it's. JavaScript在浏览器上运行(在这种情况下无论如何)并且在PHP完成它之后很久就完成了它的工作。

What you are trying to do, the way you are trying to do, simply doesn't work. 你想要做的事情,你试图做的事情,根本不起作用。 You will need to obtain the info with JS and then post it (either via a XHR request or with a form) to the server where PHP can do with it whatever it wants. 您将需要使用JS获取信息,然后将其(通过XHR请求或表单)发布到服务器,PHP可以随意使用它。 But you need to understand that that will be two entirely separated processes. 但是你需要了解这将是两个完全分离的过程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM