0

I am new here but use thee forums daily, so please forgive any etiquette errors! My question is actually very similar to this post Dynamically update variables in external PHP XML generation script . I need to pass a user input from a js var to a php script to query a db to then create an XML. I can get the file to work hardcoded. The ajax call I am using works in that it will pass a var to php using $_get, but it cant seem to output an XML file if I then try to use the passed variable unless it is hardcoded; I have this file working perfeclty in another context with hardcoded sql query. I am not sure if the query is not being passed correctly in the get method or not? maybe the code will help explain my issue a little better.

downloadUrl("XML.php"+queryString, function(data){
    var xml = data.responseXML;
    var markers = xml.documentElement.getElementsByTagName("marker");
    for (var i = 0; i < markers.length; i++) {
     var type = markers[i].getAttribute("type");
    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>" + type + "</b> <br/>" + address;
    var icon = "http://maps.google.com/mapfiles/kml/pal4/icon52.png";
    var title = markers[i].getAttribute("address")+ ' Fatalities: ' +markers[i].getAttribute("deaths");
    var marker = new google.maps.Marker({
       map: map,
       position: point,                         
       icon: icon,
       title: title
      });
     bindInfoWindow(marker, map, infoWindow, html);
                            }
  });//downloadUrl
}//load

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);
    }
  };
    var type = document.getElementById('confType').value;
    var queryString ="?type=" + type;
  request.open('GET', "XML.php"+queryString, true);
  var params='type='+type;
  request.send(params);
}

function doNothing() {}

<?php
$type = $_GET['type'];
//$type='RiotsProtests';
//$type= mysql_real_escape_string($type);

require("phpsqlajax_dbinfo.php");
$query = "SELECT * FROM incident WHERE EVENT_TYPE = `$type`"; //<-------------||||||
$result = mysql_query($query);
if (!$result) {die('Invalid query: ' . mysql_error());}

$dom = new DOMDocument("1.0");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node);
header("Content-type: text/xml");
($row = @mysql_fetch_assoc($result)){
$node = $dom->createElement("marker");
$newnode = $parnode->appendChild($node);
$newnode->setAttribute("type",$row['EVENT_TYPE']);
$newnode->setAttribute("address", $row['LOCATION']);
$newnode->setAttribute("lat", $row['LATITUDE']);
$newnode->setAttribute("lng", $row['LONGITUDE']);
$newnode->setAttribute("deaths", $row['FATALITIES']);
$newnode->setAttribute("actor1", $row['ACTOR1']);
$newnode->setAttribute("actor2", $row['ACTOR2']);
}
echo $dom->saveXML();
?>

I have tried putting in the queryString, using SESSION_vars, but nothing seems to work. Whenever i call the XML file, it doesnt seem to output anything, like the xml isnt properly getting populated. When i change the header to xml/plain, i get proper output from a hardcode query, but the xml process does not seem to like the variable. please help!!

Community
  • 1
  • 1
  • Just wondering: is there a specific reason why you're using plain JavaScript instead of jQuery? Also, take care, your SQL query is vulnerable to [SQL injections](http://www.bobby-tables.com), and you're using an outdated database API. – PLPeeters May 15 '14 at 23:31
  • Ah, I'm aware of the SQL injection. I just want to get it to function first. JavaScript is the language I started in and am open to jquery once I learn more of it. Also which API should I be using? I learn by the seat of my pants usually :p – mapsmapsMaps May 16 '14 at 23:49
  • It's just that jQuery code for AJAX calls is much more simpler than doing it all yourself. When I first started JS, I only used pure JS too, until I discovered jQuery. My opinion is that jQuery is what JavaScript should have been in the first place: simple to understand, portable and _very_ easy to use. For the database API, I personally prefer PDO, but you can go with MySQLi too. – PLPeeters May 16 '14 at 23:56

0 Answers0