簡體   English   中英

我需要將函數中的 java-script 變量的值分配給同一文件中的 php 變量

[英]I need to assign a value of java-script variable inside in function into php variable in a same file

我需要將 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 值。

您對存在的兩種語言感到困惑。 您忘記了您的 Javascript 將在客戶端的瀏覽器中運行。 PHP 將在服務器上運行。 將 JavaScript 值獲取到您的服務器的唯一方法是使用 HTTP 表單中的隱藏字段或通過 ajax 調用通過 HTTP POST/GET 將其發送到服務器。

我能想到的唯一方法是使用 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

我們在這里展示的 JavaScript 是客戶端...

由於 PHP 處於服務器端級別,因此我們必須通過 HTTP 請求訪問它。 HTTP 請求基本上是客戶端和服務器之間的通信。

JavaScript 中實現了一個名為 Ajax 的小庫,它代表異步JavaScript 和XML (XML 在這里並不重要)。

Ajax 允許我們在腳本中異步執行 HTTP 請求,並使用服務器的響應。

為了使事情在語法上更易於理解,我將使用jQuery,它提供了一個易於使用的 Ajax 庫......

有了這個,你就可以構建一個請求並相當容易地處理響應......讓我們試試這個,例如:

$.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 被設計為基於值說"Hello""Goodbye"我們發送的"testing"數據,這將提供一個非常直接的響應......在我們的例子中,它會記錄"Hello"

這展示了如何通過 Ajax 在客戶端和服務器之間創建關系,使我們能夠更輕松地來回交換數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM