这是一个完整的PHP-Javascript AJAX示例。只需使用名称保存文件即可 “myServerScript.php”并将浏览器指向它。 PHP打印javascript,它通过AJAX调用相同的PHP脚本,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; } ?>
的 现场版 强>