Reputation: 443
I am working on a project where I will read data from google maps API and convert it to XML for each object and then store that XML in an array slot (This will happen multiple times until all of the data is read) I will then make an Ajax POST request to a PHP file sending the array full of XML and a boolean (to let the server know which request it is). The PHP will then use SimpleXML to load, edit the XML with the data received, and then save it.
My problem is when I make the ajax request Firebug will recognize a POST
request and it is sending all of the correct data. It throws a 302
(error?) saying Moved Temporarily
Then a GET
request happens (which I think is initated by the server) which outputs an error saying Undefined inded: ajax
on the line where I ask if the boolean (which is called ajax) is true. And then when I check my XML file all data is deleted (the file still exists but there is no longer any XML in it) I am very confused as to how to troubleshoot this problem and I'm not really sure what to do. Here is some of my relevant code.
Thank you in advanced for any help that you can provide. Any assistance would be extremely helpful!
Also, please feel free to comment if you need me to clarify on anything.
javascript
function sendData(result)
{
$.ajax(
{
type:'POST',
url:'getXML.php',
traditional: false,
data:
{
//Result is an array full of XML
'myXML': result,
//The ajax boolean is for letting the PHP know that it is this request
'ajax' : true
},
success: function(data)
{
$('#output').text(data);
}
})
}
PHP
<?php
//Set error reporting
ini_set('display_errors', 1);
ini_set('log_errors', 1);
ini_set('error_log', dirname(__FILE__) . '/error_log.txt');
error_reporting(E_ALL);
//Checks if the get request is from the AJAX call
if($_POST['ajax'])
{
//////////////////////////////Code for editing XML/////////////////////////////////////////
//Checks to see if the xml file exists.
if (file_exists('business.xml'))
{
//Loads the XML file
$xml = simplexml_load_file('business.xml');
$result = $_POST['myXML'];
//Append XML to existing file using data loaded in from AJAX request
appendXML($xml, $result);
}
else
{
//Loading XML has failed
exit('Failed to open business.xml.');
}
}
function appendXML($xml, $xmlResult)
{
foreach($xmlResult as $result)
{
//Creates new business node
$business = $xml ->addChild('business');
//Adds XML to business node (Please oh dear god let this work)
$business ->addChild($result);
file_put_contents("business.xml", $xml);
header('Location: getXML.php');
}
}
?>
Upvotes: 0
Views: 809
Reputation: 2100
The issue you're having is with this line:
header('Location: getXML.php');
On the first run through of the page, things are working fine, and the appendXML codeblock is being called. When that happens though, you're doing a header redirect to the same page (I'm not sure why). When THIS happens, the page reloads, but in the new page non of the post data that you had set before is set. As a result $_POST is empty and your if statement fails, further $_POST['myXML'] is also empty, which would blank out your XML.
If you need to do this reload of the page for some reason, consider creating a session and storing your values in a session variable. You should also look at sanitizing the data you're passing in, as your current code has a vulnerability where someone can inject whatever information passed in from the browser into an XML file on your server. This might not be a big deal but I'm always worried about letting someone write anything unescaped into a file on my system directly. If nothing else someone could maliciously max out the space on your drive with this.
Upvotes: 1