【问题标题】:html and php file coming back emptyhtml 和 php 文件返回为空
【发布时间】:2018-05-05 07:54:04
【问题描述】:

我将文件分成 geolocation.html 和 test.php。我使用 Ajax 传递变量 var latitude 和 longitude 以在我的 php 文件中使用,但两者都返回为空。真的不需要 div 只是希望能够在我的 php 文件中使用经度和纬度。我需要在我的成功函数中添加一些东西,进行函数调用吗?我错过了什么? 地理位置.html

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type= "text/javascript" src= "js/jquery.js" > </script>
<script>

//javascript is on the client
function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition);
    } else {
        document.write("Geolocation is not supported by this browser.");
    }
}


function showPosition(position) {
     var latitude = position.coords.latitude;
     var longitude = position.coords.longitude;

     //Pass longitude and latitude to php how?
     $.ajax({
        type: 'POST',
        url: 'test.php',
        data: {lat: latitude,lng:longitude},
        success: function(data) {
            /*Pass longitude and latitude to php*/
            $("#content").html(data);
        }
    });
}

</head>     
<body onload="getLocation()">
<div id="content"></div> 
</body>

现在 test.php 文件

<?php 
 // $addlat = $_GET['addlat'];
 // $addlong = $_GET['addlong'];
 if (isset($_POST['lat']) && isset($_POST['lng']) )  {
    $lat = $_POST['lat'];
    $lng = $_POST['lng'];
    echo $lat." ".$lng  ; 
}
?>  

【问题讨论】:

  • 你在&lt;/head&gt;之前缺少&lt;/script&gt;
  • 更改数据:{lat: latitude,lng:longitude} 到数据:{'lat': latitude,'lng':longitude}
  • 为什么要调用两次jQuery?使用&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt;&lt;script type= "text/javascript" src= "js/jquery.js"&gt;&lt;/script&gt; 但不能同时使用

标签: javascript php ajax


【解决方案1】:

试试下面的代码

HTML/PHP 文件

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<body>

<p>Click the button to get your coordinates.</p>

<button onclick="getLocation()">Try It</button>

<p id="demo"></p>

<script>
var x = document.getElementById("demo");

function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(redirectToPosition);
    } else { 
        x.innerHTML = "Geolocation is not supported by this browser.";
    }
}

function redirectToPosition(position) {
    //window.location='weather.php.php?lat='+position.coords.latitude+'&long='+position.coords.longitude;

    $.ajax({
    	url: 'vuetest/testajax',
    	type: 'POST',
    	data: {lat: position.coords.latitude, lng: position.coords.longitude},
    	success : function(resp) {
    		console.log(resp)
    	}
    })
    
}
</script>

</body>
</html>

服务器端

public function testajax()
    {
        print_r($_POST);
      // Here you can check and use variables
    }

【讨论】:

  • 你应该解释为什么 OP 应该尝试这个。你有什么改变,为什么?您可能还应该提到此代码用于调试,而不是用于复制/粘贴以使其工作。
  • 非常感谢你们。但我仍然不能在我的 .php 文件中使用我的变量
  • 另外,由于某种原因,您的服务器端代码示例似乎是类方法?那是什么类,它会在哪里被调用? OP没有提到任何框架或类似的......
  • @IsmailIssa 你怎么能这么肯定?
  • 在我的 html 文件中,long 和 lat 输出到 div 但是当我刷新 php 页面时返回空(通过 html 后)
【解决方案2】:

这对我来说非常有效。

  1. 我删除了其中一个 jQuery 包含。
  2. 我使用了 jQuery $(document).ready() 而不是 body onload 事件。

HTML 文件

<html>
<head>
    <script type="text/javascript" src="js/jquery.js"></script>
    <script type="text/javascript">
    //javascript is on the client
    function getLocation() {
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(showPosition);
        } else {
            document.write("Geolocation is not supported by this browser.");
        }
    }

    function showPosition(position) {
        var latitude = position.coords.latitude;
        var longitude = position.coords.longitude;

        //Pass longitude and latitude to php how?
        $.ajax({
            type: 'POST',
            url: 'test.php',
            data: {lat: latitude,lng:longitude},
            success: function(data) {
                /*Pass longitude and latitude to php*/
                $("#content").html(data);
            }
        });
    }

    $(document).ready(function() {
        getLocation();
    });
    </script>
</head>   
<body>
    <div id="content"></div> 
</body>
</html>

PHP 文件

<?php 
if (isset($_POST['lat']) && isset($_POST['lng']) )  {
    $lat = $_POST['lat'];
    $lng = $_POST['lng'];
    echo $lat." ".$lng  ; 
}
?>

【讨论】:

    猜你喜欢
    • 2015-10-08
    • 2012-07-15
    • 1970-01-01
    • 2015-06-20
    • 2018-12-04
    • 1970-01-01
    • 2013-08-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多