繁体   English   中英

如何在JavaScript中传递会话变量

[英]How pass session variable in javascript

我是php的新手...我为html编写了javascript。但是我不知道如何使用sesssion值将相同的javascript概念写入php。

我的会话值:

$_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); } }); } }); } 

任何人的帮助

如果您将该JavaScript函数与PHP文件一起使用,则可以执行以下操作:

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

要将JavaScript变量传递给PHP,您必须执行以下步骤:

  1. 在页面中包含JQuery库 (如果还没有的话)。
  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)
 });

尝试这个 -

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

是的。获取您的会话变量$ _SESSION ['line2'],$ _ SESSION ['oline2']

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

现在将这些值保存在html隐藏类型输入中

“ />” />

现在在您的JavaScript代码中调用这些隐藏的值。

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

在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);
                        }
                    });
                }
            });
        }   

暂无
暂无

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

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