Dynamic XML
From Google Mapki
[edit] Replacing static XML files with dynamic data
In the Google Maps API documentation, the Using XML and Asynchronous RPC ("AJAX") with Maps example demonstrates getting point data from an XML file. Dynamic data can in fact be used, the line:
request.open("GET", "data.xml", true);
could actually read
request.open("GET", "data.php", true);
and everything would work as long as the PHP code returned a page that was an XML document
This is easily acheived in most languages using the echo or print statement. However, the document should also have an XML header else your var xmlDoc = request.responseXML; command will not work as there is no XML. To not use XML you could use something like var txtDoc = request.responseText; to return text and somehow encode a text string with the data. Also, if the data changes but the URL stays the same, unless directives are put in the script, the browser will cache the old result and you will not see any changes to the data.
For example, you have a PHP script that queries an SQL database and returns records from it as XML. The map app issues a request.open("GET", "data.php", true), the map is displayed. The back end SQL database has a record added, and the map calls request.open("GET", "data.php", true) again.
Unless you put a directive in the PHP script to tell the browser to not cache the page, you will not see the new record The solution to both these issues for PHP is to add the following lines to the start of the PHP script:
// Date in the past
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
// always modified
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
// HTTP/1.1
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
// HTTP/1.0
header("Pragma: no-cache");
//XML Header
header("content-type:text/xml");
[edit] XMLHTTPRequest ("AJAX" Transmission of data to/from a back end script)
To send data to a back-end script (eg ASP, PHP etc) the Google Maps API call GXmlHttp.create() is used as per the documentation, but here is some extra info that wasn't mentioned:
Primer: JavaScript is a Client Side language meaning it runs on the machine the browser is running on, whereas (for example) PHP is a Server Side Language, meaning code runs on the web server. Exchanging variables between them is not generally possible. The workaround is to use XMLHTTPRequest to send data to or from the server.
To send variables, use GET mode and encode them in standard & and ? fashion
request.open("GET", "data.php?variable1=value1&variable2=value2&variable3=value3", true);
As far as I am aware, the length of the URL string in a GET is limited to around 500 characters, so is only really useful for sending variables that would control what is sent back, rather than sending data.
Make sure any ? and & characters in the URL string that are part of the variables are encoded to thier % equivalent (ie %26 instead of &). Example, if you wanted to send variable "action" with value "in&out" you would use:
request.open("GET", "data.php?var1=value1&action=in%26out", true);
Javascript to do this would be:
variablename.value.replace ( /&/g, '%26' )
To send data, use POST mode and encode the data to be inserted using & to separate the fields.
Example:
request.open('POST', "data.php?action=addpoint" , true);
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
request.send('PointLat=0.12345&PointLng=1.23456&PointName=This%26That&PointInfowindow='+document.myform.textbox_html.value.replace ( /&/g, '%26' ) + '&PointCategory=1');
Notice how the first field is not preceeded by a & and also that & symbols within data must be replaced with %26. (ie This%26That instead of This&That)
