【问题标题】:PHP and Javascript value passingPHP 和 Javascript 值传递
【发布时间】:2015-07-20 08:33:38
【问题描述】:

我正在使用 Html5 地理定位来获取用户的坐标并通过 Javascript 将它们发送到外部 PHP 文件以反向定位地址:

xhr.open( 'GET', 'utility/fetch.php?lat='+lat+'&lng='+lng, true );

fetch.php 上,我正在反向定位街道地址以及我需要解析响应并将其发回的任何内容。

function geo_callback(r){
    var result = //parsing r and getting required data
    /*
     some code here for declaring variables and stuff
    */
    //printing the result from database 
    xmlhttp.onreadystatechange=function() {
            if (xmlhttp.readyState==4 && xmlhttp.status==200) {
                document.getElementById("result").innerHTML=xmlhttp.responseText;
            }
    }
    //sending result to another external php to fetch data from database
    xmlhttp.open("GET", "utility/coord_to_add.php?result="+result ,true);
    xmlhttp.send(); 
}

现在我的问题是,这是一种有效的方法吗?让我们忽略我只是提供我在这里使用的方法的所有错误处理。另外,我意识到我作为结果返回并在“结果”div 中打印的数据不应用 CSS。

【问题讨论】:

  • 是的。关于所有要说的
  • 您可能希望 PHP 输出 JSON,这样将来添加更多信息也会更容易,并且 JavaScript 也易于处理。
  • 但奇怪的是,css 没有应用,假设我将数据库中的结果回显为: echo "
    ".$someData."
    ";尽管 .customDIV 存在,但没有应用 css

标签: javascript php html geolocation


【解决方案1】:

我认为使用某种 MVC 框架会更有效(在网络资源和时间方面),并在调用 "utility/coord_to_add.php?result="+result 期间添加您添加到数据库的信息作为调用的一部分'utility/fetch.php?lat='+lat+'&lng='+lng 代替。如果您在

中提供更多数据
/*
 some code here for declaring variables and stuff
*/

部分(操作 var result),那么我建议您将所有数据提供给单个端点,而不是使用您在此处使用的多阶段方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-01
    • 2018-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    相关资源
    最近更新 更多