Reputation: 143
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
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:
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
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
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
Reputation: 31749
Try this -
var variable = "<?php echo $_SESSION['variable'];?>";
Upvotes: 0