PHP和Javascript的值传递

PHP and Javascript value passing

本文关键字:值传 Javascript PHP      更新时间:2023-09-26

我使用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(); 
}

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

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

的一部分
/*
 some code here for declaring variables and stuff
*/

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