2017-01-18 12 views
0

Google APIのこのコード。検索ボックス "pac-input"から値を取得し、mysqlデータベースに挿入したい。入力ボックスからどのように価値を得ることができますか?私はすでに$_GET$_POST$_REQUESTを試みたが、これが原因である を動作しませんでした:https://developers.google.com/maps/documentation/javascript/examples/places-searchbox入力ボックスからデータを取得し、mysqlデータベースに挿入

<input id="pac-input" class="controls" type="text" placeholder="Search Box" name="pac-input"> 
<div id="map"></div> 
<script> 

    function initAutocomplete() { 
    var map = new google.maps.Map(document.getElementById('map'), { 
     center: {lat: -33.8688, lng: 151.2195}, 
     zoom: 13, 
     mapTypeId: 'roadmap' 
    }); 

    // Create the search box and link it to the UI element. 
    var input = document.getElementById('pac-input'); 
    var searchBox = new google.maps.places.SearchBox(input); 
    map.controls[google.maps.ControlPosition.TOP_LEFT].push(input); 



    // Bias the SearchBox results towards current map's viewport. 
    map.addListener('bounds_changed', function() { 
     searchBox.setBounds(map.getBounds()); 
    }); 

    var markers = []; 
    // Listen for the event fired when the user selects a prediction and retrieve 
    // more details for that place. 
    searchBox.addListener('places_changed', function() { 
     var places = searchBox.getPlaces(); 

     if (places.length == 0) { 
     return; 
     } 

     // Clear out the old markers. 
     markers.forEach(function(marker) { 
     marker.setMap(null); 
     }); 
     markers = []; 

     // For each place, get the icon, name and location. 
     var bounds = new google.maps.LatLngBounds(); 
     places.forEach(function(place) { 
     if (!place.geometry) { 
      console.log("Returned place contains no geometry"); 
      return; 
     } 
     var icon = { 
      url: place.icon, 
      size: new google.maps.Size(71, 71), 
      origin: new google.maps.Point(0, 0), 
      anchor: new google.maps.Point(17, 34), 
      scaledSize: new google.maps.Size(25, 25) 
     }; 

     // Create a marker for each place. 
     markers.push(new google.maps.Marker({ 
      map: map, 
      icon: icon, 
      title: place.name, 
      position: place.geometry.location 
     })); 

     if (place.geometry.viewport) { 
      // Only geocodes have viewport. 
      bounds.union(place.geometry.viewport); 
     } else { 
      bounds.extend(place.geometry.location); 
     } 
     }); 
     map.fitBounds(bounds); 
    }); 
    } 

</script> 
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB9RRjfBs5H4kP7Pa-1SePt6FzrzmC6KX8&libraries=places&callback=initAutocomplete" 
    async defer></script> 

答えて

0

$_GET, $_POST and $_REQUESTが唯一のフォーム送信後に働くだろう。あなたはその前の入力フィールドの値にアクセスする場合は、入力し、「PAC-入力」の値を取得したい場合は、直接、input.value

0

を行うことができ、JavaScriptで次のコマンドを使用します。

var search_box = document.getElementById('pac-input').value; 
+0

どのように私はそのvarのsearch_boxをPHP変数に追加できますか?おかげで – ble

+0

あなたは、このようにそれを行うことができます。 または 'ます。 ' .ANDは、次のコードを置く: ' ' –

+0

私はそれをしたときに、PHPエコー"オブジェクトHTMLInputElement " – ble

関連する問題