appu
appu

Reputation: 143

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..

My session value:

$_SESSION['line2'],

$_SESSION['oline2']

My javascript code here:

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

Upvotes: 0

Views: 12031

Answers (4)

Guilherme Ferreira
Guilherme Ferreira

Reputation: 143

If you are using that JavaScript function with a PHP file, you can do it:

...
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:

  1. Include JQuery Library in your page (if you don't have yet).
  2. Create a PHP page to set the variables (ie: myPage.php).
  3. Pass the JS variables with ajax (JQuery) to the myPage.php via POST.

myPage.php:

<?php
session_start();

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

In your JS function:

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

Upvotes: 3

Vikram
Vikram

Reputation: 3351

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. It can not be placed in .js file

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

Upvotes: 0

SuReSh
SuReSh

Reputation: 1511

yup.. get your session variable $_SESSION['line2'], $_SESSION['oline2']

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

now save these value in html hidden type input

" /> " />

now call these hidden value in your javascript code..

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

Upvotes: 0

Sougata Bose
Sougata Bose

Reputation: 31749

Try this -

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

Upvotes: 0

Related Questions