0

I get "documentElement of null" error. I am trying to follow https://developers.google.com/maps/articles/phpsqlajax_v3 UPDATE: On using dev tools it seems that the "downloadUrl()" function has problems.

My "phpsqlajax_genxml.php" create_element("markers"); $parnode = $doc->append_child($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
global $wpdb ;
//$query = "SELECT * FROM markers WHERE 1";
$query = $wpdb->get_results("SELECT * FROM `markers` WHERE 1;") ;
/*no need of this
$result = mysql_query($query);
if (!$result) {
  die('Invalid query: ' . mysql_error());
}*/

header("Content-type: text/xml");

// Iterate through the rows, adding XML nodes for each
 foreach ($query as $q) 
  {
  // ADD TO XML DOCUMENT NODE
  echo '<marker ';
  echo 'name="' . parseToXML($q->name) . '" ';
  echo 'address="' . parseToXML($q->address) . '" ';
  echo 'lat="' . $q->lat . '" ';
  echo 'lng="' . $q->lng . '" ';
  echo 'type="' . $q->type . '" ';
  echo '/>';
}
    ?>

My "hourly.php" file which takes xml from above file

 <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY"
        type="text/javascript"></script>
<script type="text/javascript">
//<![CDATA[

var customIcons = {
  restaurant: {
    icon: 'http://labs.google.com/ridefinder/images/mm_20_blue.png'
  },
  bar: {
    icon: 'http://labs.google.com/ridefinder/images/mm_20_red.png'
  }
};

function load() {
  var map = new google.maps.Map(document.getElementById("map"), {
    center: new google.maps.LatLng(47.6145, -122.3418),
    zoom: 13,
    mapTypeId: 'roadmap'
  });
  var infoWindow = new google.maps.InfoWindow;

  // Change this depending on the name of your PHP file
  downloadUrl("phpsqlajax_genxml.php", function(data) {
    var xml = data.responseXML;
    var markers = xml.documentElement.getElementsByTagName("marker");
    for (var i = 0; i < markers.length; i++) {
      var name = markers[i].getAttribute("name");
      var address = markers[i].getAttribute("address");
      var type = markers[i].getAttribute("type");
      var point = new google.maps.LatLng(
          parseFloat(markers[i].getAttribute("lat")),
          parseFloat(markers[i].getAttribute("lng")));
      var html = "<b>" + name + "</b> <br/>" + address;
      var icon = customIcons[type] || {};
      var marker = new google.maps.Marker({
        map: map,
        position: point,
        icon: icon.icon
      });
      bindInfoWindow(marker, map, infoWindow, html);
    }
  });
}

function bindInfoWindow(marker, map, infoWindow, html) {
  google.maps.event.addListener(marker, 'click', function() {
    infoWindow.setContent(html);
    infoWindow.open(map, marker);
  });
}

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, request.status);
    }
  };

  request.open('GET', url, true);
  request.send(null);
}

function doNothing() {}

//]]>

  • It seems that xml is not getting loaded or is invalid. Help is appreciated:) – khursheed Ali Oct 29 '16 at 17:47
  • you can't put script tags in a .js file – Milo Oct 29 '16 at 18:45
  • Are you sure that the xml is correctly load, i think you need to adjust the path to the file (with wp_localize_script) – Benoti Oct 29 '16 at 21:11
  • @Benoti.. Can u give me an example or snippet of how u do that – khursheed Ali Oct 30 '16 at 6:15
0

Figured it out!!!!! Since we get the SQL query result with the help of "$wpdb" object we can access each row's attribute as an associative array. As a result there isn't a need for "mysql_fetch_assoc()" and "mysql_query()" functions. Also we need to define "$wpdb" object, so we require "wp-config.php" file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.