简体   繁体   English

如何在JavaScript中传递会话变量

[英]How pass session variable in javascript

Im new for php... I wrote javascript for html.. But i dont know how to write this same javascript concept to php using that sesssion value.. 我是php的新手...我为html编写了javascript。但是我不知道如何使用sesssion值将相同的javascript概念写入php。

My session value: 我的会话值:

$_SESSION['line2'],

$_SESSION['oline2']

My javascript code here: 我的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); } }); } }); } 

Anyone help 任何人的帮助

If you are using that JavaScript function with a PHP file, you can do it: 如果您将该JavaScript函数与PHP文件一起使用,则可以执行以下操作:

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

To pass JavaScript variable to the PHP, you have to follow these steps: 要将JavaScript变量传递给PHP,您必须执行以下步骤:

  1. Include JQuery Library in your page (if you don't have yet). 在页面中包含JQuery库 (如果还没有的话)。
  2. Create a PHP page to set the variables (ie: myPage.php). 创建一个PHP页面来设置变量(即:myPage.php)。
  3. Pass the JS variables with ajax (JQuery) to the myPage.php via POST. 通过POST将带有ajax(JQuery)的JS变量传递到myPage.php。

myPage.php: myPage.php:

<?php
session_start();

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

In your JS function: 在您的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'];?>";

yup.. get your session variable $_SESSION['line2'], $_SESSION['oline2'] 是的。获取您的会话变量$ _SESSION ['line2'],$ _ SESSION ['oline2']

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

now save these value in html hidden type input 现在将这些值保存在html隐藏类型输入中

" /> " /> “ />” />

now call these hidden value in your javascript code.. 现在在您的JavaScript代码中调用这些隐藏的值。

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

Before using php code within javascript code you need to make sure that your javascript code is written in .php file or another file that can be rendered as php. 在javascript代码中使用php代码之前,您需要确保您的javascript代码是写在.php文件或其他可以呈现为php的文件中。 It can not be placed in .js file 不能放在.js文件中

Try following code 尝试以下代码

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