John Smith John Smith - 4 months ago 11
PHP Question

JavaScript not recognized inside PHP

Someone plese tell me what I did wrong? Because, by it's self the js code works. When I put it inside php it's not recognized.

Is the if condition wrong or it's the quotes. I don't understand where is the problem.

Is just an PHP if statement that checks if we are on that page run that js code. I tried with heredoc instead of quotes but the result is the same.

if ($_SERVER['REQUEST_URI'] == 'http://us-airquality.com/index.php/locations/') {
echo '
<script>
window.onload = getMyLocation;

// var ourCoords = {
// latitude : 47.624851,
// longitude: -122.52099
// }
var cities = {
"1":{
"name":"Jacksonville",
"coords":{
latitude : 30.3322,
longitude: 81.6557
},
"url":"http://us-airquality.com/index.php/locations/jacksonville/"
},
"2":{
"name":"SF Peninsula",
"coords":{
latitude : 37.4615,
longitude: 122.3108
},
"url":"http://us-airquality.com/index.php/locations/sfpeninsula/"
},
"3":{
"name":"Atlanta",
"coords":{
latitude : 33.7490,
longitude: 84.3880
},
"url":"http://us-airquality.com/index.php/locations/atlanta/"
},
"4":{
"name":"Maryland",
"coords":{
latitude : 39.0458,
longitude: 76.6413
},
"url":"http://us-airquality.com/index.php/locations/maryland/"
},
"5":{
"name":"Houston",
"coords":{
latitude : 29.7604,
longitude: 95.3698
},
"url":"http://us-airquality.com/index.php/locations/huston/"
},
"6":{
"name":"San Jose",
"coords":{
latitude : 37.20,
longitude: 121.54
},
"url":"http://us-airquality.com/index.php/locations/san-joseeast-bay/"
},
"7":{
"name":"New Jersey",
"coords":{
latitude : 40.0583,
longitude: 74.4057
},
"url":"http://us-airquality.com/index.php/locations/new-jersey/"
},
"8":{
"name":"Seattle",
"coords":{
latitude : 47.6062,
longitude: 122.3321
},
"url":"http://us-airquality.com/index.php/locations/seattle/"
},
"9":{
"name":"Dallas",
"coords":{
latitude : 32.7767,
longitude: 96.7970
},
"url":"http://us-airquality.com/index.php/locations/dallas/"
},
"10":{
"name":"Pennsylvania",
"coords":{
latitude : 41.2033,
longitude: 77.1945
},
"url":"http://us-airquality.com/index.php/locations/pennsylvania/"
},
"11":{
"name":"Chicago",
"coords":{
latitude : 41.8781,
longitude: 87.6298
},
"url":"http://us-airquality.com/index.php/locations/chicago/"
},
"12":{
"name":"New Jersey – North",
"coords":{
latitude : 40.0583,
longitude: 74.4057
},
"url":"http://us-airquality.com/index.php/locations/new-jersey-north/"
},
"13":{
"name":"Orlando",
"coords":{
latitude : 28.5383,
longitude: 81.3792
},
"url":"http://us-airquality.com/index.php/locations/orlando/"
},
"14":{
"name":"San Jose",
"coords":{
latitude : 25.7617,
longitude: 80.1918
},
"url":"http://us-airquality.com/index.php/locations/miamibrowardwp/"
}
};


function getMyLocation() {
// check if the browser supports Geolocation API
if (navigator.geolocation) {
// we call the getCurrentPosition method and pass in a handler function, displayLocation
navigator.geolocation.getCurrentPosition(displayLocation, displayErrors);
} else {
alert("No geolocation support by your browser!");
}
}


// Function called when the browser has a location
// position contains the latitude and longitude of your location
function displayLocation(position) {
// grab the latitude and longitude of your location from the position object and his .coords property
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;

// var div = document.getElementById("location");
// div.innerHTML = "You are at Latitude: " + latitude + ", Longitude: " + longitude;

//var km = computeDistance(position.coords, ourCoords);

var arrKMs = [];
for (var city in cities)
{
//var cityName=cities[city].name;
var cityCoords=cities[city].coords;

arrKMs[city] = computeDistance(position.coords, cityCoords);
var kmm= computeDistance(position.coords, cities[city].coords);
}

//get minimal value
var index = 0;
var value = 100000000;
for (var ind in arrKMs)
{//alert(ind + " - " + arrKMs[ind]);
if (parseFloat(arrKMs[ind]) < value)
{
value = arrKMs[ind];
index = ind;
}
}
window.location.replace(cities[index].url);
//alert(cities[index].url);
// var distance = document.getElementById("distance");
// distance.innerHTML = "You are " + arrKMs[index] + " km from our Company";

// var url_address = document.getElementById("url_address");
// url_address.innerHTML = "web site found: " + cities[index].url;

// showMap(position.coords);
}


// Handler which is passed an error by the Geolocation API
function displayErrors(error) {
var errorTypes = {
0: "Unknown error",
1: "Permision denied by user",
2: "Position is not available... ",
3: "Request timed out"
};

// using the error.code property, we assign one of those strings(0, 1, 2, 3) to a new variable, errorMessage
var errorMessage = errorTypes[error.code];

// In the case of errors zero and two, there is sometimes additional information in the error.message property, so we add that to our errorMessage string
if (error.code == 0 || error.code == 2) {
errorMessage = errorMessage + " " + error.message;
}
var div = document.getElementById("location");
// we add the message to the page to let the user know
div.innerHTML = errorMessage;
}


// This function takes two coordinates, a start coodinate and a destination coordinate, and returns the distance in kilometers between them
function computeDistance(startCoords, destCoords) {
var startLatRads = degreesToRadians(startCoords.latitude);
var startLongRads = degreesToRadians(startCoords.longitude);
var destLatRads = degreesToRadians(destCoords.latitude);
var destLongRads = degreesToRadians(destCoords.longitude);

// radius of the Earth in km
var Radius = 6371;
var distance = Math.acos(Math.sin(startLatRads) * Math.sin(destLatRads) +
Math.cos(startLatRads) * Math.cos(destLatRads) *
Math.cos(startLongRads - destLongRads)) * Radius;

return distance;
}


function degreesToRadians(degrees) {
var radians = (degrees * Math.PI)/180;
return radians;
}

var map;
function showMap(coords) {
var googleLatAndLong = new google.maps.LatLng(coords.latitude,coords.longitude);
var mapOptions = {
zoom: 10,
center: googleLatAndLong,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var mapDiv = document.getElementById("map");
map = new google.maps.Map(mapDiv, mapOptions);

// add the user marker
var title = "Your Location";
var content = "You are here: " + coords.latitude + ", " + coords.longitude;
addMarker(map, googleLatAndLong, title, content);
}


// shows with a pin where are you
function addMarker(map, latlong, title, content) {
var markerOptions = {
position: latlong,
map: map,
title: title,
clickable: true
};
var marker = new google.maps.Marker(markerOptions);

var infoWindowOptions = {
content: content,
position: latlong
};

var infoWindow = new google.maps.InfoWindow(infoWindowOptions);

google.maps.event.addListener(marker, \'click\', function() {
infoWindow.open(map);
});
}
</script>';


}

Zac Zac
Answer

$_SERVER['REQUEST_URI'] doesn't typically include the domain, unless you're using a proxy server. Try this for your if condition instead:

if ($_SERVER['REQUEST_URI'] == '/index.php/locations/') {
Comments