javascript - 从输入框获取数据并插入到mysql数据库

标签 javascript php mysql

此代码来自 Google API。我想从搜索框“pac-input”获取值,然后插入到 mysql 数据库。如何从输入框中获取值?我已经尝试过 $_GET$_POST$_REQUEST 但没有成功 这是来源:https://developers.google.com/maps/documentation/javascript/examples/places-searchbox

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

最佳答案

$_GET、$_POST 和 $_REQUEST 仅在表单提交后才起作用。如果你想访问之前输入字段的值,可以直接执行 input.value

关于javascript - 从输入框获取数据并插入到mysql数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41719928/

相关文章:

javascript - 从另一个组件打开模态点击 react js

php - 安全地存储从Docker容器访问的MySQL凭据

mysql - Wordpress 超慢服务器响应 11GB Wordpress 数据库

MySQL - INSERT ON DUPLICATE KEY - 2列

mysql - 如何仅在MYSQL 中不存在数据时才插入数据?

javascript - openlayers 4 从 ol.layer.Vector 获取特征

javascript - 限制与 AngularJS 状态匹配的动态 URL 参数的最佳方法

javascript - Angular 2 - 更好地使用 Angular-cli 还是在样板之前准备干净?

php 声称我定义的变量未定义

javascript - 使用 PHP 在浏览器中有条件地呈现 Javascript 函数调用