【问题标题】:I need to assign a value of java-script variable inside in function into php variable in a same file我需要将函数内部的 java-script 变量的值分配给同一文件中的 php 变量
【发布时间】:2015-06-12 10:35:21
【问题描述】:

我需要将 computeTotalDistance() 函数中的总值放入 php 变量中。我尝试了很多方法,但毫无意义。

            var rendererOptions = {
                draggable: true
            };
            var directionsDisplay = new google.maps.DirectionsRenderer(rendererOptions);
            ;
            var directionsService = new google.maps.DirectionsService();
            var map;

            var Srilanka = new google.maps.LatLng(6.9344, 79.8428);

            function initialize() {

                var mapOptions = {
                    zoom: 8,
                    center: Srilanka
                };
                map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
                directionsDisplay.setMap(map);

                google.maps.event.addListener(directionsDisplay, 'directions_changed', function () {
                    computeTotalDistance(directionsDisplay.getDirections());
                });

                calcRoute();
            }

            function calcRoute() {

                var locations = ['Galle', 'Kandy', 'minneriya wildlife park', 'Horton Plains'];

                var waypoints = locations.map(function(loc) {
                    return { 'location': loc };
                });

                var request = {
                    origin: 'Colombo',
                    destination: 'Colombo',
                    waypoints: waypoints,
                    travelMode: google.maps.TravelMode.DRIVING
                };


                directionsService.route(request, function(response, status) {
                    if (status == google.maps.DirectionsStatus.OK) {
                        directionsDisplay.setDirections(response);
                    }
                });
            }

            function computeTotalDistance(result) {
                var total = 0;
                var myroute = result.routes[0];
                for (var i = 0; i < myroute.legs.length; i++) {
                    total += myroute.legs[i].distance.value;
                }
                total = total / 1000.0;
                document.getElementById('total').innerHTML = total + ' km';
            }

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

        </script>

我使用了以下一个,但我对如何用它捕获返回值感到困惑。

 <?php
      $tot = echo "<script>document.writeln();</script>";
  ?> 

我如何将这个总距离值分配给 $tot 值。

【问题讨论】:

  • PHP 是服务器端,JavaScript 是(通常)客户端。

标签: javascript php google-maps


【解决方案1】:

您对存在两种语言感到困惑。您忘记了您的 Javascript 将在客户端、浏览器中运行。 PHP 将在服务器上运行。将 JavaScript 值发送到服务器的唯一方法是使用 HTTP 表单中的隐藏字段或通过 ajax 调用将其 HTTP POST/GET 到服务器。

【讨论】:

  • 谢谢阿里,但是我的目标不熟悉ajax,如果可以请给我一个小解释,我该怎么做??非常感谢
【解决方案2】:

我能想到的唯一方法是使用 JavaScript 将带有参数的 URL 发回,例如:

http://www.example.com/index.php?id=1

...通过使用这样的东西,你可以在 JavaScript 中应用它:

window.location.href="index.php?value=1";

然后在PHP代码中使用$_GET:

$somevar = $_GET["value"]; //puts the uid varialbe into $somevar

【讨论】:

  • 有了这个,我们可以在不重新加载页面的情况下将这个值赋给 php 变量吗?
  • 恐怕不刷新你就无法做到这一点。
  • 接受答案 :) 如果它对你有帮助
  • @ChathurangaGammanpila,实际上可以在不重新加载的情况下执行此类操作。检查我的答案。你需要实现一个叫做 Ajax 的东西。
【解决方案3】:

我们在这里展示的 JavaScript 是客户端...

由于 PHP 处于服务器端级别,我们必须通过 HTTP 请求来访问它。 HTTP 请求基本上是客户端和服务器之间的通信。

JavaScript 有一个方便的小库,称为 Ajax,它代表 Asynchronous JavaScript 和 XML(XML 在这里并不重要)。

Ajax 允许我们在脚本中异步执行 HTTP 请求,并使用服务器的响应。

为了让语法更容易理解,我将使用jQuery, which provides an easy-to-use Ajax library...

有了这个,你可以很容易地构造一个请求并处理响应......让我们试试这个例子:

$.ajax({
  "url": "index.php",
  "type": "POST",
  "data": {"testing":true},
  "success": function(response){
    console.log(response);
  }
});

这提供了我们的index.php文件,并说我们有以下代码:

<?php
  if(isset($_POST["testing"])) {
    echo $_POST["testing"] ? "Hello" : "Goodbye";    
  }
?>

现在回到 Ajax 函数......在"success" 处理程序中,我们告诉它记录任何响应......因为我们的 PHP 被设计为基于基于我们发送的"testing" 数据的值,这将提供一个非常直接的响应......在我们的例子中,它会记录"Hello"

这说明了如何通过 Ajax 在客户端和服务器之间创建关系,从而使我们能够更轻松地来回交换数据。

【讨论】:

  • 谢谢Jamen,我对ajax不熟悉,对了,如果我在computeTotalDistance()函数内的total变量下使用这个$.ajax()
猜你喜欢
  • 2022-11-27
  • 2019-05-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多