关于ajax将数据发布到PHP

时间:2018-01-23 14:41:26

标签: php ajax html5 geolocation

我是Ajax的新手,我知道当我想将数据传递给PHP文件时我需要使用Ajax,但我不确定Ajax可以像我编码的那样使用。如果不能,任何人都可以帮助我吗?因为我想使用html5地理位置来获取用户位置。我之前尝试过geoplugin,但我得到的IP总是服务器IP而不是用户IP,我试着在这里询问地理路由但没有工作。我已经尝试过这个html5地理定位来显示我的lon和lat,它是正确的,但我想将变量传递给另一个PHP文件,以便计算距离MySQL的数据最近的距离。

在index.php中

<script>
$(document).ready(function(){
if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(showLocation);
} else { 
    $('#location').html('Geolocation is not supported by this browser.');
}
});

function showLocation(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
$.ajax({
    type:'POST',
    url:'index1.php',
    data:'latitude='+latitude+'&longitude='+longitude
    }
});
}
</script>

在index1.php中

<?php
session_start();
$link=mysqli_connect("localhost","id2135226_ukwai1203","ukwai1203");
mysqli_select_db($link,"id2135226_demo");
$lat=$_POST['latitude'];
$lon=$_POST['longitude'];
$sql = "select branch_id,(6371 * 2 * ASIN(SQRT( POWER(SIN(($lat - 
branch_lat)*pi()/180/2),2)+COS($lat*pi()/180 
)*COS(branch_lat*pi()/180)*POWER(SIN(($lon-branch_lon)*pi()/180/2),2)))) 
as 
distance From Branch ORDER BY distance ASC LIMIT 1";
$res=mysqli_query($link,$sql);
while($row=mysqli_fetch_array($res)){
$_SESSION['location']=$row['branch_id'];
?>
<script>

window.location="http://ukwai1203.000webhostapp.com/fyp/user/shop.php";
</script>
<?php
}
?>

1 个答案:

答案 0 :(得分:-3)

将您的代码更改为:

public class FluxUtil {

    private static String s = "";

    public static void main(String[] args) {

        Disposable flux = Flux.just("a", "b", "c")
                .map(String::toUpperCase)
                .doOnNext(FluxUtil::print)
                .subscribe();
    }

    private static Object print(Object o) {

        s = !s.isEmpty() ? s.concat("->") : s;
        s = s.concat(o.toString());
        System.out.println(s);
        return o;
    }
}

考虑使用像eclipse这样的IDE来帮助你找到丢失的括号等。 当通过修改旧的代码检查库版本,如jquery版本。