2013-11-28 3 views
2

내 PHP 페이지에서 나는 그가 내 사이트에 올 때 사용자 도시를 저장하고 있습니다.구글 API PHP 내부의 자바 스크립트 코드

여기 Google API가 웹에서 가져 왔습니다.

<script type="text/javascript" src="http://www.google.com/jsapi?key=ABQIAAAAp04yNttlQq-7b4aZI_jL5hQYPm-xtd00hTQOC0OXpAMO40FHAxQMnH50uBbWoKVHwgpklyirDEregg"></script> 
    <script type="text/javascript"> 
     if(google.loader.ClientLocation) 
     { 
      visitor_lat = google.loader.ClientLocation.latitude; 
      visitor_lon = google.loader.ClientLocation.longitude; 
      visitor_city = google.loader.ClientLocation.address.city; 
      visitor_region = google.loader.ClientLocation.address.region; 
      visitor_country = google.loader.ClientLocation.address.country; 
      visitor_countrycode = google.loader.ClientLocation.address.country_code; 
      document.getElementById('yourinfo').innerHTML = '<p>Lat/Lon: ' + visitor_lat + '/' + visitor_lon + '</p><p>Location: ' + visitor_city + ', ' + visitor_region + ', ' + visitor_country + ' (' + visitor_countrycode + ')</p>'; 
     } 
     else 
     { 
      document.getElementById('yourinfo').innerHTML = '<p>Whoops!</p>'; 
     } 
    </script> 

PHP 변수로 저장해야합니다. 어떤 사람이 PHP 페이지에서 어떻게 사용할 수 있는지 말해 줄 수 있습니까?

나는 전통적인 방법을 알고있다. echo 모두 <html> to </html> 그 안에 코드를 넣는다. 그러나 다른 더 좋은 방법을 알고 싶습니까?

또한 PHP 변수에 javascript 변수를 할당하는 방법을 모르겠습니다.

+0

당신은 아약스를 통해 그 작업을 수행 할 수 있습니다 http://api.jquery.com/jQuery.ajax/과 어떻게 실행을 요청하는 경우 다음 서버에 ... – Leonardo

+0

을 자신의 위치를 ​​저장 이 JavaScript 코드는'.php' 파일에 담기 만하면됩니다. 간단히''태그의 바깥에 넣을 수 있습니다 . – Charles

+0

[참고 : 내 자바 스크립트에서 PHP (또는 다른 서버 측) 코드가 작동하지 않는 이유는 무엇입니까?] (http://stackoverflow.com/questions/13840429/reference-why-does-the-php-or- 다른 서버 측 코드 내 자바 스크립트가 아닌) –

답변

1

또한 javascript var 값을 PHP 값으로 정렬하는 방법을 알지 못합니다.

Javascript가 클라이언트, 브라우저 및 PHP에서 서버로 실행되기 때문에 그렇게 할 수 없습니다.

1

당신은 쉽게 PHP에서 IP 주소에서 방문자의 위치 정보를 얻을 수 있습니다 :

자세한 내용
<?php 
require_once("userip/ip.codehelper.io.php"); 
require_once("userip/php_fast_cache.php"); 

$_ip = new ip_codehelper(); 

$real_client_ip_address = $_ip->getRealIP(); 
$visitor_location  = $_ip->getLocation($real_client_ip_address); 

$guest_ip = $visitor_location['IP']; 
$guest_country = $visitor_location['CountryName']; 
$guest_city = $visitor_location['CityName']; 
$guest_state = $visitor_location['RegionName']; 

echo "IP Address: ". $guest_ip. "<br/>"; 
echo "Country: ". $guest_country. "<br/>"; 
echo "State: ". $guest_state. "<br/>"; 
echo "City: ". $guest_city. "<br/>"; 

?> 

는 여기 http://www.a2zwebhelp.com/visitor-location-in-php

1
자바 스크립트가 실행되기 때문에 당신은 PHP 변수에 자바 스크립트 변수 값을 할당 할 수 없습니다

클라이언트 Borwser에서 PHP 스크립트는 서버에서 실행됩니다.

그러나 다시 서버로 전송하는 방법을 POST를 사용하거나 GET의 방법이 ..

당신은 그것을 할 AJAX/JQuery와 사용할 수 있습니다.

Jquery를 사용하는 방법 중 하나가 가장 쉽습니다. 여기에 링크입니다 : http://api.jquery.com/jQuery.ajax/

가 서버에 PHP 파일 만들기는 당신이 얻을 후 다음 코드를 추가

<?php 
$city = $_GET['city']; 
$user = $_GET['name']; 
// Write your code to store the variable $city into the database 
?> 

을 storecity.php 전화 사용자의 도시 (이것에 대한 JQuery와 라이브러리를 포함합니다 작품은)

$.ajax({ 
    type: "GET", 
    url: "storecity.php", 
    data: { name: "John", city: "Boston" } 
});