Nolski
Nolski

Reputation: 443

Trouble with appending XML in PHP that was passed through AJAX

I have posted another question for the same project which was helpfully answered but I seem to have run into another problem which seem different enough that it warrants a separate question. Any help would be greatly appreciated and thank you in advanced if you can provide any.

Okay, so first let me describe what I am doing. I am using the google Maps Javascript v3 API with the places library to find nearby restaurants & some extra information on them. The data for each search result (restaurant in this case) is stored in a JSON object. Now a co-worker needs this data in XML and stored on our web server so his program can access and read it. So I use a jQuery JSON2XML plugin (http://goessner.net/download/prj/jsonxml/) to convert that JSON to an XML string. I then use a jQuery ajax function to push the data to a php file which should save it. Here is the javascript code:

function sendData(result)
{
    $.ajax(
    {
        type:'POST',
        url:'getXML.php',
        data:
        {
            'myXML': result,
            'ajax' : true
        },
        success: function(data)
        {
            $('#output').text(data);
        }
    })
}

Then from there the data should get sent over to my PHP file (getXML.php) and I have code written that will open up an existing XML file (using SimpleXML in PHP5) and then append the XML coming from the AJAX call. It will then save the XML file and it will all happen again for each restaurant. (So it will be sending multiple AJAX POST requests to the server.) Here is my PHP code:

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

            //Append XML to existing file using data loaded in from AJAX request
            appendXML($xml);    
        } 
        else 
        {
            //Loading XML has failed
            exit('Failed to open business.xml.');
        }
    }

    function appendXML($xml)
    {
        //Creates new business node
        $business = $xml ->addChild('business');

        //Adds XML to business node (Please oh dear god let this work)
        $business ->addChild($_POST['myXML']);

        file_put_contents("business.xml", $xml);
        header('Location: form_upload.php');
    }
?>

Okay, that was sort of a lot but let me get to the part where I tell you my problem. I am new to PHP so I am not entirely sure what is going wrong here but when I POST my data to the server I get two errors for each ajax request.

The first is:

PHP Warning:  simplexml_load_file() [<a href='function.simplexml-load-file'>function.simplexml-load-file</a>]: business.xml:1: parser error : Document is empty in /home/mpn3712/www/309/gPlaces/getXML.php on line 16

The second is

PHP Fatal error:  Call to a member function addChild() on a non-object in /home/mpn3712/www/309/gPlaces/getXML.php on line 31

Thank you again for all of the help!

Upvotes: 0

Views: 276

Answers (1)

Matt
Matt

Reputation: 7040

$xml is not in scope when it's referenced in appendXML(). You need to pass it through a parameter.

Also, be sure to exit() after you write a Location header.

if (file_exists('business.xml')) 
{
    //Loads the XML file
    $xml = simplexml_load_file('business.xml');

    //Append XML to existing file using data loaded in from AJAX request
    appendXML($xml);    
} 
. 
. 
.
function appendXML($xml)
{
    //Creates new business node
    $business = $xml ->addChild('business');

    //Adds XML to business node (Please oh dear god let this work)
    $business ->addChild($_POST['myXML']);

    file_put_contents("business.xml", $xml);
    header('Location: form_upload.php');
    exit();
}

Here's documentation on variable scope from the PHP manual.

Upvotes: 1

Related Questions