2016-07-27 153 views
0

有人告诉我我做错了什么?因为,由于它本身就是js代码的作品。当我把它放在php里面时,它不被识别。JavaScript中无法识别的PHP

是否条件错误或它是引号。我不明白问题在哪里。

只是一个PHP if语句,用于检查我们是否在该页面上运行该js代码。我试着用heredoc而不是引号,但结果是一样的。

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>'; 

}

+0

'REQUEST_URI' [从PHP文档(http://php.net/manual/en/reserved.variables.server.php)_“的URI这是为了访问此页面;例如,'/index.html'。“_这意味着你正在比较'http:// us-airquality.com/index.php/locations /'到'index.php/locations /'所以,你的if子句正在做你正在做的事情,做一些基本的调试和研究对于编码是必不可少的。 – Epodax

+0

添加'else {echo“错误:URL是”。 $ _SERVER [ 'REQUEST_URI']; }'if'后进行简单的调试 –

+0

我得到了同样的错误:URL是index.php/locations /,仍然不能用于$ _SERVER ['REQUEST_URI'] =='http:// us-airquality。 com/index.php/locations /')'或'$ _SERVER ['REQUEST_URI'] =='index.php/locations /') –

回答

1

$_SERVER['REQUEST_URI']通常不包含域,除非您使用的是代理服务器。试试这个你if条件,而不是:

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

谢谢@Zac和所有帮助我的人。 –

+0

不客气!更好的方法是'if($ _SERVER ['PATH_INFO'] =='/ locations /'){ – Zac