javascript - 如何将数据从 javascript 发送到 PHP 脚本

标签 javascript php mysql

我在 js 中将数据传输到 PHP 的代码(不起作用)是:

function codeAddress() {
  var address = document.getElementById('address').value;
  geocoder.geocode({ 'address': address}, function(results, status) {
   if (status == google.maps.GeocoderStatus.OK) {
  map.setCenter(results[0].geometry.location);
  var marker = new google.maps.Marker({
      map: map,
      position: results[0].geometry.location

  }); 


  var input = results[0].geometry.location; 

 var lat = input.lat(); 
 console.log(lat); 

/*var lat =  input.lat();
var longitude = input.lng();
document.getElementById("wypisz").innerHTML=lat;
request.open("GET", "address.php?lat=" + lat, true);

        //var url = "address.php?lat=latitude&lng =longitude";  


//document.getElementById("wypisz").innerHTML = results[0].geometry.location.lng;

*/



$.ajax({
    type: "GET",
    data: "lat=" + lat,
    url:"address.php",
    success: function(data) { console.log(data) }
})

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

并将它们放入 PHP 文件中:

if(isSet($_GET['lat'])){
    echo "param good";
} else {
    echo "incorrect";   
}

当我print_r它时,它总是显示空数组。 你能帮我完成这个转换吗?

这是我的更多代码,这是我标记特定纬度和经度的地方:

 <div id="panel">
  Address :
  <input id="address" name = "address" type="textbox" value="Warszawa, Pol">

  <input type="button" value="wstaw" onclick="codeAddress()" >

</div>
<div id="map-canvas"></div>

这里我将信息发送到 php 文件:

<input type="submit" id = "ff" value="Send information">

最佳答案

var req = new XMLHttpRequest(); 
req.open("GET", "adress.php", true);
req.onreadystatechange = function() {
    if(req.readyState != 4 || req.status != 200) 
        return; 
    console.log(req.responseText);
};
req.send("lat=" + lat);

这是一种极其简单的方法,如果您想要有关错误验证等的所有功能,我强烈建议您使用 jquery 和 $.get:

$.ajax({
        type: "GET",
        data: "lat=" + lat,
        url:"address.php",
        success: function(data) { console.log(data) }
    })

关于javascript - 如何将数据从 javascript 发送到 PHP 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22142309/

相关文章:

javascript - Internet Explorer 未加载某些 .js 文件

javascript - 使用 jest fake 计时器测试 redux-saga debounce

PHP检查推荐网址是否为主页

php - 我应该使用 user_id (主键)还是 user_name (唯一索引)从 Mysql 插入和获取数据

mysql - 在 Php MySQL 中执行的查询,但不在 Zend Framework 中执行

javascript - 切换div以显示隐藏

javascript - 如何将 X 轴标签旋转到条形图中的条形图上?

php - Wordpress - 同时使用 meta_query 和 tax_query?

php - 如何修复此 MySQL 查询

mysql - 使用主键将 Ignore 插入 Mysql