2012-02-20 8 views
1

좌표 값을보기에서 컨트롤러로 전송하려고합니다. add google api에서 가져온 좌표이지만 문제가 있습니다.이 스크립트는 내가보기에서 사용하고 있습니다. _add. 자바 스크립트 콘솔은 좌표를 잘 출력하기 때문에 스크립트가 작동합니다.게시 방법을 통해보기에서 컨트롤러로 좌표를 전달합니다.

<script type="text/javascript"> 

     $(document).ready(function(){ 
      get_location(); 
     }); 

     function get_location() 
     { 
      if(navigator.geolocation) 
      { 
       navigator.geolocation.getCurrentPosition(function(position){ 

         var latitude = position.coords.latitude; 
         var longitude = position.coords.longitude; 

         $.post('http://firstaid:8888/add', { latitude:latitude, longitude:longitude }); 

       }); 
      } 
     } 

    </script> 

이제이 뷰에서 컨트롤러로 값을 전달하여 PHP codeigniter 컨트롤러에서 변수로 사용할 수 있습니다. 이렇게하려면 $this->input->post을 사용합니다. 실수는 내가 $latitude을 인쇄 할 때

function index() 
{ 
    $data['title'] = 'Add'; 

    $latitude = $this->input->post('latitude'); 

    $this->load->view('templates/_header', $data); 
    $this->load->view('_add'); 

} 

하지만, 변수가 될 수 blank.where입니다? 아마 내가보기를로드하기 전에 게시물을 호출하기 때문에?

데이터를 컨트롤러로 전달하는 또 다른 방법이 있습니까? 이것에

$.post('http://firstaid:8888/add', { latitude:latitude, longitude:longitude }); 

:

답변

0

는이 라인을 변경해야

$.post('http://firstaid:8888/add', 'latitude='+latitude+'&longitude='+longitude); 

고양이를 피부 하나 개 이상의 방법이 내가 jQuery를 방법이 생각으로, 물론, 거기를 그 JSON을 적절한 POST 변수 문자열 형식으로 serialize 할 수 있지만 여기에있는 것이 확실히 작동합니다.

+0

는 불행히도 결과 ... 나는 ($ 위도) 인 print_r을 시도하지; 주사위(); 하지만 결과가 없습니다 ... –

+0

PHP'$ _POST' 배열을 직접 검사 해 보셨습니까? –

+0

print_r ($ _ POST ('latitude'))을 시도했습니다. 하지만 난이 오류가 나타납니다 : 치명적인 오류 : 함수 이름은 문자열이어야합니다 몇 가지 아이디어? –

0

보기에 위도를 전달하지 않으므로 액세스 할 수 없습니다.

새 페이지를로드하는 경우 $ latitude는 뷰에 전달되는 $ 데이터 배열에 있어야합니다.

그러나 Ajax를 사용하고 있으므로 새로운보기를로드 할 수 없습니다.

$latitude = $this->input->post('latitude'); 
echo json_encode($latitude); 

그런 다음 자바 스크립트로 응답을 처리하십시오.

또는 컨트롤러에서 직접 print_r을 사용하여 변수가 있는지 확인하십시오. ,

$.ajax({ 
    type: "POST", 
    url: "/add", 
    data: "latitude=" + latitude, 
    dataType: "json", 
    success: function(msg){ 
     alert('Completed'); 
    } //End success condition 
}); //End ajax 

가 크롬이나 파이어 버그에서 Ajax 요청을 할 때 "네트워크"패널 그래서 당신이 컨트롤러를 찾는 것을 확인할 수 열려 있는지 확인하십시오

print_r($this->input->post('latitude')); 

당신의 자바 스크립트이보십시오.

+0

나는 거짓을 ... 왜? –

+0

먼저 이것을 시도해보십시오 : print_r ($ this-> input-> post ('latitude')); – cszy

+0

항상 공백 값 ... 잘 모르겠다 ... –

0

컨트롤러 파일 :

$this->load->library('googlemaps'); 

$config['center'] = '37.4419, -122.1419'; 
$config['zoom'] = 'auto'; 
$this->googlemaps->initialize($config); 

$marker = array(); 
$marker['position'] = '37.429, -122.1419'; 
$marker['draggable'] = true; 
$marker['ondragend'] = 'updateDatabase(event.latLng.lat(), event.latLng.lng());'; 
$this->googlemaps->add_marker($marker); 
$data['map'] = $this->googlemaps->create_map(); 

$this->load->view('view_file', $data); 

보기 파일 :

<html> 
<head> 
    <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script> 
    <script type="text/javascript"> 
     function updateDatabase(newLat, newLng) 
     { 
      // make an ajax request to a PHP file 
      // on our site that will update the database 
      // pass in our lat/lng as parameters 
      $.post(
       "/my_controller/update_coords", 
       { 'newLat': newLat, 'newLng': newLng, 'var1': 'value1' } 
      ) 
      .done(function(data) { 
       alert("Database updated"); 
      }); 
     } 
    </script> 
    <?php echo $map['js']; ?> 
</head> 
<body><?php echo $map['html']; ?></body> 
</html> 

에 의해 참조 : http://biostall.com/demos/google-maps-v3-api-codeigniter-library/updatedatabase

관련 문제