0

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

5
  • you want to pass this value to javascript function? Commented Apr 22, 2015 at 10:48
  • It will be good if you can mention what is your requirement and what you are trying to do with code Commented Apr 22, 2015 at 10:58
  • My js having this line... document.getElementsByName("addressline2")[0].value.... I dont know how to pass session value here. Commented Apr 22, 2015 at 11:00
  • 1
    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 Commented Apr 22, 2015 at 11:08
  • Answered below with php code between javascript code. Commented Apr 22, 2015 at 11:08

4 Answers 4

3

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)
 });
Sign up to request clarification or add additional context in comments.

4 Comments

@appu Just copy that two line above and replace where you want, like: document.getElementsByName("addressline2")[0].value --> line2;
var km = drivingDistanceKilometers; How this variable value send to session
So do you want pass a JavaScript variable to the PHP Session variable ?
Updated! Try again... :)
0

Try this -

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

Comments

0

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>

1 Comment

or try this var line2 = '<?php echo $_SESSION["line2"]; ?>'; var oline2 = '<?php echo $_SESSION["oline2"]; ?>';
0

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

6 Comments

var km = drivingDistanceKilometers; How this variable value send to session
What does it mean ? You want to use a php variable to assign value in km or set the km variable value in session ? Can you be more specific ?
set the km variable value in session
You might need to use AJAX to save its value to session. Where in code you want to save its value in session ?
I dont know ajax... can u help me
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.