0

我想做的是从我的谷歌地图中获取以下信息。我知道 JavaScript 是客户端,而不是像 PHP 那样的服务器端,所以它甚至在 JS 运行之前就被加载了。问题是如果我要严格通过 PHP -> XML -> JS 将列 post_id 调用到 PHP 中,它不会加载链接。有没有办法只用 PHP 来做到这一点,并让它从标记表中为地图上的每个位置标记提取位置 post_id?我也读过 AJAX 是可能的,但我对 AJAX 了解不多。

boxText.innerHTML = "<div class='mapname'>" + '<a href="<?php echo get_permalink( $post_id ); ?>">' + name + '</a>' + "</div><i>" + listtype + "</i> <br/>" + address + "<br/>" + phone;

谢谢!

4

1 回答 1

0

这是一个完整的 PHP-Javascript AJAX 示例。只需保存名为“myServerScript.php”的文件并将浏览器指向它。PHP 打印通过 AJAX 调用相同 PHP 脚本的 javascript,PHP 响应并且 javascript 处理响应。

确保您的 PHP 安装配置为接受短分隔符<?...?>

<?

if(isset($_GET['msg'])){
    print "alert('AJAX response from PHP - Javascript said:". $_GET['msg']."')";
}
else{
    print <<<qq
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<title>AJAX Example: (by Marcelo)</title>
<script src="http://maps.google.com/maps/api/js?sensor=false"></script>


<script>
// Sorry, I can't be bothered typing "google.maps." every time. ;-)
var G = google.maps;

var zoom = 4;
var centerPoint = new G.LatLng(40.178873, -96.767578);
var container;
var map;


function ajaxLoad(url,callback,plain) {
    var http_request = false;

    if (window.XMLHttpRequest) { // Mozilla, Safari, ...
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType && plain) {
            http_request.overrideMimeType('text/plain');
        }
    } else if (window.ActiveXObject) { // IE
        try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }
    if (!http_request) {
        alert('Giving up :( Cannot create an XMLHTTP instance');
        return false;
    }
    http_request.onreadystatechange =  function() {
        if (http_request.readyState == 4) {
            if (http_request.status == 200) {
                eval(callback(http_request));
            }
            else {
                alert('Request Failed: ' + http_request.status);
            }
        }
    };
    http_request.open('GET', url, true);
    http_request.send(null);
}
var url = "myServerScript.php?msg=hello from javascrpt";
ajaxLoad(url, parseResults, true);


function parseResults(req){
    var text = req.responseText;
    eval(text);
}

function load() {
    container = document.getElementById('mapDiv');

    var myOptions = {
        zoom: zoom,
        center: centerPoint,
        mapTypeId: G.MapTypeId.ROADMAP,
        rotateControl: true,
        keyboardShortcuts:false
    }
    map = new G.Map(container, myOptions);
 google.maps.event.addListener(map,'click',function(mev){
var url = "myServerScript.php?msg="+mev.latLng;
ajaxLoad(url, parseResults, true);    
  });
}

window.onload = load;

</script>
</head>
<body>
Hello from PHP.
<h2>Click on the map to send the lat/lon to PHP</h2>
<div id="mapDiv" style="width: 800px; height: 450px;"></div>
</body>
</html>

qq;

}


?>

现场版在这里

于 2012-08-28T05:56:02.370 回答