Jump to content

PHP to delete XML element


Anaximines

Recommended Posts

Hi, I have what I hope is a simple question and I apologize if it has been covered somewhere else but I searched around and couldn't find anything.

 

I'm trying to use a PHP file (deleteMarker.php) to search a XML file full of google maps markers with the following structure:

 

<marker lat="XXXXX" lng="YYYYY" comment="ZZZZZ"/><marker .../>...

 

for the marker with the lat value matching that of a JavaScript variable (markerlatitude) and then remove that marker. Here is the JavaScript and PHP that I have so far (init() just reinitializes the map):

 

function deleteMarker() {
var markerlatitude = document.getElementById("markerlatitude").value;

var getVar =  "?markerlatitude=" + markerlatitude;

var request = GXmlHttp.create();

//open the request to deleteMarker.php on server
request.open('GET', 'deleteMarker.php' + getVar, true);
request.onreadystatechange = function() {
	if (request.readyState == 4) {
		init();
	}
}
request.send(null);
return false;
}

 

<?php
header('Content-Type: text/xml;charset=UTF-8');

$markerlatitude = (float)$_GET['markerlatitude'];

$xdoc = new DOMDocument();
$xdoc->load('data.xml');
$lats = $xdoc->getElementsByTagName('marker')->item(0);
foreach ($lats as $lat) {
if ($lat == $markerlatitude) {
	$xdoc->documentElement->removeChild($lat);
	}
}
$xdoc->save('data.xml');
?>

 

Any help I could get on this would be greatly appreciated, I'm really not sure why it's not working.

 

Thanks,

Eric

Link to comment
https://forums.phpfreaks.com/topic/162574-php-to-delete-xml-element/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.