View Full Version : Help adding Drop down menu PHP/MYSQL Ajax call

03-30-2011, 04:46 AM
I am very new and could totally use a little help with this.

I have an ajax call that generates xml and is initialized on page load. It works great right now but I need to add in a drop down menu that will allow user to select and change which db it connects to then reinitialize the ajax call and generate new xml from the selected db without page refresh.

I don't want to dynamically fill the drop down options just pass along the preset option value selected with the url and re-initialize the call.

Any help will be very much appreciated.. Much respect to the knowledgeable veterans on here that always get it done.. I tried a few things but it just wasn't working so I took it out..

I figured that I need to ask for some help with this.. Thank In advance for any suggestions you all may have..

the code is as follows:

Ajax Call

downloadUrl("scripts/phpsqlajax_genxml.php", function(data) {
var xml = parseXml(data);
var markers = xml.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markers.length; i++) {
var lat = parseFloat(markers[i].getAttribute("lat"));
var lng = parseFloat(markers[i].getAttribute("lng"));
var point = new google.maps.LatLng(lat,lng);
var address = markers[i].getAttribute("address");
var name = markers[i].getAttribute("name");
var html = "<b>"+name+"<\/b><p>"+address;
var category = markers[i].getAttribute("category");
var marker = createMarker(point,name,html,category);



function downloadUrl(url, callback) {
var request = window.ActiveXObject ?
new ActiveXObject('Microsoft.XMLHTTP') :
new XMLHttpRequest;

request.onreadystatechange = function() {
if (request.readyState == 4) {
request.onreadystatechange = doNothing;
callback(request.responseText, request.status);

request.open('GET', url, true);

function parseXml(str) {
if (window.ActiveXObject) {
var doc = new ActiveXObject('Microsoft.XMLDOM');
return doc;
} else if (window.DOMParser) {
return (new DOMParser).parseFromString(str, 'text/xml');

function doNothing() {}

Below is phpsqlajax_genxml.php


require ("dbinfo.php");

// Start XML file, create parent node

$dom = new DOMDocument("1.0");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node);

// Opens a connection to a MySQL server

$connection=mysql_connect ('localhost', $username, $password);
if (!$connection) { die('Not connected : ' . mysql_error());}

// Set the active MySQL database

$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
die ('Can\'t use db : ' . mysql_error());

// Select all the rows in the markers table

$query = "SELECT * FROM markers WHERE 1";
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());

header("Content-type: text/xml encoding=\"utf-8\"");

// Iterate through the rows, adding XML nodes for each

while ($row = @mysql_fetch_assoc($result)){
$node = $dom->createElement("marker");
$newnode = $parnode->appendChild($node);
$newnode->setAttribute("address", $row['address']);
$newnode->setAttribute("lat", $row['lat']);
$newnode->setAttribute("lng", $row['lng']);
$newnode->setAttribute("category", $row['category']);

echo $dom->saveXML();

mysql_free_result ($result);


Below is dbinfo.php