我想从数据库中获取值并将它们放在XML临时文件中(对于Google Maps webapp),然后将它们分配给输入值。
如果查询是静态的,我知道怎么做,例如Select * from markers where 1
,但我想做Select * from markers WHERE ID = $id
之类的事情,而这个$ id来自POST。
我有一个带JS的HTML文件,它调用PHP存档来获取XML并解析它。它适用于静态查询,但如何使这些查询动态化?如何将该变量从JS函数传递给我的PHP文件?
这是获取XML的代码:
<?php
$username="SOMEUSER";
$password="SOMEPASSS";
$database="SOMEUSER_marcadoresMapas";
function parseToXML($htmlStr)
{
$xmlStr=str_replace('<','<',$htmlStr);
$xmlStr=str_replace('>','>',$xmlStr);
$xmlStr=str_replace('"','"',$xmlStr);
$xmlStr=str_replace("'",''',$xmlStr);
$xmlStr=str_replace("&",'&',$xmlStr);
return $xmlStr;
}
// 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 clave";
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
header("Content-type: text/xml");
// Start XML file, echo parent node
echo '<markers>';
// Iterate through the rows, printing XML nodes for each
while ($row = @mysql_fetch_assoc($result)){
// ADD TO XML DOCUMENT NODE
echo '<marker ';
echo 'name="' . parseToXML($row['name']) . '" ';
echo 'address="' . parseToXML($row['address']) . '" ';
echo 'lat="' . $row['lat'] . '" ';
echo 'lng="' . $row['lng'] . '" ';
echo 'telefono="' . $row['telefono'] . '" ';
echo 'zona="' . $row['zona'] . '" ';
echo 'ciudad="' . $row['ciudad'] . '" ';
echo 'email="' . $row['email'] . '" ';
echo 'piso="' . $row['piso'] . '" ';
echo 'tipo="' . $row['tipo'] . '" ';
echo 'erasmus="' . $row['erasmus'] . '" ';
echo 'nhabitaciones="' . $row['nhabitaciones'] . '" ';
echo 'plazas="' . $row['plazas'] . '" ';
echo 'equipHabita="' . $row['equipHabita'] . '" ';
echo 'nbanos="' . $row['nbanos'] . '" ';
echo 'salon="' . $row['salon'] . '" ';
echo 'cocina="' . $row['cocina'] . '" ';
echo 'electrodomesticos="' . $row['electrodomesticos'] . '" ';
echo 'garaje="' . $row['garaje'] . '" ';
echo 'internet="' . $row['internet'] . '" ';
echo 'calefaccion="' . $row['calefaccion'] . '" ';
echo 'sexo="' . $row['sexo'] . '" ';
echo 'precio="' . $row['precio'] . '" ';
echo 'superficie="' . $row['superficie'] . '" ';
echo 'fecha="' . $row['fecha'] . '" ';
echo 'otros="' . $row['otros'] . '" ';
echo 'id="' . $row['id'] . '" ';
echo '/>';
}
//echo 'name="' . parseToXML('&','&', $row['name']) . '" ';
// End XML file
echo '</markers>';
?>
这是调用PHP文件并获取XML的JS:
function downloadUrl(url, callback) {
var request = window.ActiveXObject ?
new ActiveXObject('Microsoft.XMLHTTP') :
new XMLHttpRequest; //en cierto modo es una API, acepta requests HTTP.
request.onreadystatechange = function() {
if (request.readyState == 4) {
request.onreadystatechange = doNothing;
callback(request.responseText, request.status);
}
};
request.open('GET', url, true);
request.send(null);
}
downloadUrl("phpsqlajax_genxml2.php", function(data) {
var xml = parseXml(data);
var markers = xml.documentElement.getElementsByTagName("marker"); //coge todos los markers
for (var i = 0; i < markers.length; i++) { //coge los atributos de los markers
var name = markers[i].getAttribute("name");
var address = markers[i].getAttribute("address");
var telefono= markers[i].getAttribute("telefono");
var precio=markers[i].getAttribute("precio");
//var type = markers[i].getAttribute("type");
var point = new google.maps.LatLng( //crea LatLng a partir de Lat Long de los markers
parseFloat(markers[i].getAttribute("lat")),
parseFloat(markers[i].getAttribute("lng")));
var html = "Nombre:" + name + "<br>Direccion:" + address+"<br>Telefono:"+telefono+"<br>Precio:"+precio;
var marker = new google.maps.Marker({ //posiciona los markers
map: map,
position: point
});
bindInfoWindow(marker, map, infoWindow, html); //pone la ventana de información
}
});
}
我想做的是像函数downloadUrl(url,callback,id),并通过POST发送ID。
这可能或我应该使用哪种方法?
非常感谢,很抱歉我没有很好的解释。
答案 0 :(得分:1)
在JavaScript端,您可以为downloadUrl()函数添加第三个参数。并将open命令设置为使用POST而不是GET
request.open('POST', url, true);
并使用请求发送id
var params = 'id='+id;
request.send(params);
在PHP端捕获并过滤传入的请求变量。并将其添加到查询中。
$id = filter_input(INPUT_POST, 'id', FILTER_SANITIZE_NUMBER_INT);
$query = "SELECT * FROM markers WHERE id = {$id}";