将javascript变量传递给创建XML的php文件

时间:2014-05-15 23:12:49

标签: javascript php mysql ajax xml

我是新来的,但每天使用你的论坛,所以请原谅任何礼仪错误!我的问题实际上非常类似于这篇文章Dynamically update variables in external PHP XML generation script。我需要将用户输入从js var传递到php脚本以查询db然后创建XML。我可以让文件硬编码工作。我使用的ajax调用的工作原理是它会使用$ _get将var传递给php,但如果我尝试使用传递的变量,它似乎无法输出XML文件,除非它是硬编码的;我有这个文件在硬编码的SQL查询的另一个上下文中完成perfeclty。我不确定查询是否未在get方法中正确传递?也许代码将有助于更好地解释我的问题。

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();
?>

我尝试使用SESSION_vars输入queryString,但似乎没有任何效果。每当我调用XML文件时,它似乎都没有输出任何东西,比如xml没有正确填充。当我将标题更改为xml / plain时,我从硬编码查询中获得正确的输出,但xml过程似乎不喜欢变量。请帮助!!

0 个答案:

没有答案