2015-04-22 38 views
0

進出口新的PHP ...我寫的JavaScript會話變量HTML ..但我不知道如何寫這個相同的JavaScript概念使用sesssion值到PHP ..如何通過在javascript

我的會話值:

$_SESSION['line2'], 

$_SESSION['oline2'] 

我的JavaScript代碼在這裏:

function showLocation() { 
 
      var geocoder, location1, location2, gDir; 
 

 
      geocoder = new GClientGeocoder(); 
 
      gDir = new GDirections(); 
 
      GEvent.addListener(gDir, "load", function() { 
 
       //var drivingDistanceMiles = gDir.getDistance().meters/1609.344; 
 
       var drivingDistanceKilometers = gDir.getDistance().meters/1000; 
 
       // var miles = drivingDistanceMiles ; 
 
       var km = drivingDistanceKilometers; 
 
       //document.getElementById('miles').value = miles; 
 
       document.getElementById('km').value = km; 
 
       
 
      }); 
 

 
geocoder.getLocations(document.getElementsByName("addressline2")[0].value, function (response) { 
 
       if (!response || response.Status.code != 200) 
 
       { 
 
        alert("Sorry, we were unable to geocode the first address"); 
 
       } 
 
       else 
 
       { 
 
        location1 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address}; 
 
        geocoder.getLocations(document.getElementsByName("officeaddressline2")[0].value, function (response) { 
 
         if (!response || response.Status.code != 200) 
 
         { 
 
          alert("Sorry, we were unable to geocode the second address"); 
 
         } 
 
         else 
 
         { 
 
          location2 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address}; 
 
          gDir.load('from: ' + location1.address + ' to: ' + location2.address); 
 
         } 
 
        }); 
 
       } 
 
      }); 
 
     } 
 
    

任何幫助

+0

? –

+0

這將是很好,如果你可以提到什麼是你的要求和你想要做什麼與代碼 –

+0

我的js有這條線... document.getElementsByName(「addressline2」)[0] .value ....我沒有知道如何在這裏傳遞會話價值。 – appu

回答

2

如果您使用的是JavaScript函數與PHP文件,你可以做到這一點:

... 
var line2 = '<?php echo $_SESSION["line2"]; ?>'; 
var oline2 = '<?php echo $_SESSION["oline2"]; ?>'; 
... 

要通過JavaScript變量的PHP,你必須遵循以下步驟:

  1. 包括JQuery Library在您的網頁(如果你還沒有)。
  2. 創建一個PHP頁面來設置變量(即:myPage.php)。
  3. 通過POST將帶有ajax(JQuery)的JS變量傳遞給myPage.php。

myPage.php:

<?php 
session_start(); 

$_SESSION['line2'] = $_POST['myVariable']; 
$_SESSION['oline2'] = $_POST['anotherOne']; 
... 

在你的JS功能:

var sth = 'something...'; 
var sthMore = 'something more...'; 

$.ajax({ 
    method: "POST", 
    url: "myPage.php", 
    data: { myVariable: sth, anotherOne: sthMore } 
}).done(function() { 
    // alert('done!'); // here you can alert some message (if you want) 
}); 
+0

請編輯我的完整編碼併發送給我.. – appu

+0

@appu只需複製上面的兩行並替換您想要的位置,如:document.getElementsByName(「addressline2」)[0] .value - > line2; –

+0

var km = drivingDistanceKilometers;如何將此變量值發送到會話 – appu

0

試試這個 -

var variable = "<?php echo $_SESSION['variable'];?>"; 
0

燁.. 得到您的會話變量 $ _SESSION ['line2'],$ _SESSION ['oline2']

<?php 
$line2 = $_SESSION['line2']; 
$oline2= $_SESSION['oline2']; 
?> 
現在

保存HTML隱藏式輸入這些值

「/> 」/>

現在請在你的JavaScript代碼,這些隱藏的價值..

<script> 
var line2= $("#line2").val(); 
var oline2= $("#oline2").val(); 
</script> 
+0

或試試這個 var line2 ='<?php echo $ _SESSION [「line2」]; ?>'; var oline2 ='<?php echo $ _SESSION [「oline2」]; ?>'; –

0

之前使用JavaScript代碼內的PHP代碼,你需要確保你的JavaScript代碼是用.php文件或另一個可以呈現爲php的文件編寫的。它不能被放置在js文件

嘗試下面的代碼

function showLocation() { 
      var geocoder, location1, location2, gDir; 

      geocoder = new GClientGeocoder(); 
      gDir = new GDirections(); 
      GEvent.addListener(gDir, "load", function() { 
       //var drivingDistanceMiles = gDir.getDistance().meters/1609.344; 
       var drivingDistanceKilometers = gDir.getDistance().meters/1000; 
       // var miles = drivingDistanceMiles ; 
       var km = drivingDistanceKilometers; 
       //document.getElementById('miles').value = miles; 
       document.getElementById('km').value = km; 

      }); 

geocoder.getLocations("<?php echo $_SESSION['line2'];?>", function (response) { 
       if (!response || response.Status.code != 200) 
       { 
        alert("Sorry, we were unable to geocode the first address"); 
       } 
       else 
       { 
        location1 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address}; 
        geocoder.getLocations("<?php echo $_SESSION['oline2'];?>", function (response) { 
         if (!response || response.Status.code != 200) 
         { 
          alert("Sorry, we were unable to geocode the second address"); 
         } 
         else 
         { 
          location2 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address}; 
          gDir.load('from: ' + location1.address + ' to: ' + location2.address); 
         } 
        }); 
       } 
      }); 
     } 
要將此值傳遞給javascript函數
+0

var km = drivingDistanceKilometers;這個變量值如何發送到會話 – appu

+0

這是什麼意思?你想使用一個php變量來指定以km爲單位的值,或者在會話中設置km變量值?你可以說得更詳細點嗎 ? –

+0

在會話 – appu