douduo2407 2015-04-07 13:42
浏览 85
已采纳

将变量从html传递到javascript到php

I'm trying to geocode a user submitted address and store it into a database. The form calls a php file where javascript retrieves the address and geocodes it. Then the lat and lng values are passed to php and stored in a database, however the only values in the database are zeros.

The HTML file:

 <html>
  <body>
    <form action="registerEvent.php" id="form" method="post">       
        <input id="address" name="address" placeholder="Adrese" type="text">                    
        <button type="submit" id="submit">Send</button>             
    </form>
  </body>
</html>

registerEvent.php:

    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true"></script>
<script>
var geocoder;
var inputLat;
var inputLng;

function codeAddress() {

    geocoder = new google.maps.Geocoder();

  var address = document.getElementById('address').value;
  geocoder.geocode( { 'address': address}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {

    var inputLat = event.latLng.lat(); 
    var inputLng = event.latLng.lng();    

    } else {
      alert('Geocode was not successful for the following reason: ' + status);
    }
  });

}

function passvariable() { 
  window.location.href = "registerEvent.php?lat=" + inputLat;
  window.location.href = "registerEvent.php?lng=" + inputLng; 
}

    codeAddress();
    passvariable();

</script>



<?php
    require("dbinfo.php");

    $connection=mysqli_connect ('localhost', $username, $password);
if (!$connection) {
  die('Not connected : ' . mysql_error());
}

$db_selected = mysqli_select_db($connection, $database);
if (!$db_selected) {
  die ('Can\'t use db : ' . mysql_error());
}

    $lat =$_GET['inputLat']; 
    $lng =$_GET['inputLng']; 

    $sql = "INSERT INTO sometable (lat, lng)
VALUES ('$lat', '$lng')";

    if (!mysqli_query($connection,$sql)) {
      die('Error: ' . mysqli_error($connection));
    };

    mysqli_close($connection);
?>
  • 写回答

4条回答 默认 最新

  • douqiaotong8682 2015-04-07 13:49
    关注

    You are calling window.location.href twice and the variables seem incorrect in the PHP part ($_GET['inputLat'] instead of lat, and $_GET['inputLong'] instead of lon)

    If the HTML file is a separate file, then you need to get the address in a different way: replace

    var address = document.getElementById('address').value;
    

    with

    var address = "<?php echo $_POST['address']; ?>"
    

    The JS functions should look more like this

    <script src="https://maps.googleapis.com/maps/api/jsv=3.exp&signed_in=true">
    </script>
    <script> 
    var geocoder;
    var inputLat;
    var inputLng; 
    
    function codeAddress() {
      geocoder = new google.maps.Geocoder();
      var address = document.getElementById('address').value;
      geocoder.geocode( { 'address': address}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        var inputLat = event.latLng.lat(); 
        var inputLng = event.latLng.lng();    
        window.location.href = "registerEvent.php?lat=" + inputLat + "&lng=" + inputLng;
        } else {
          alert('Geocode was not successful for the following reason: ' + status);
        }
      });
    
    }
    codeAddress();
    </script>
    

    And the PHP part:

        // ...
        $lat =$_GET['lat']; 
        $lng =$_GET['lng']; 
    
        if (!empty($lat) && !empty($lng)) {    
           $sql = "INSERT INTO sometable (lat, lng) VALUES ('$lat', '$lng')";
           if (!mysqli_query($connection,$sql)) {
             die('Error: ' . mysqli_error($connection));
           };
        }
    
        mysqli_close($connection);
    

    If everything is in the same file, the form button should be changed to something like this:

    <button type="button" id="submit" onclick="codeAddress()">Send</button>    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突