如何在javascript

时间:2015-04-22 10:44:03

标签: javascript php

我是新的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);
                        }
                    });
                }
            });
        }   
      

任何帮助

4 个答案:

答案 0 :(得分: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。
  4. 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)
     });
    

答案 1 :(得分:0)

试试这个 -

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

答案 2 :(得分:0)

烨.. 得到你的会话变量 $ _SESSION ['line2'],$ _SESSION ['oline2']

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

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

“/&gt; “/&gt;

现在在您的javascript代码中调用这些隐藏值..

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

答案 3 :(得分: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);
                        }
                    });
                }
            });
        }