Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8442541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:52:11+00:00 2026-06-10T08:52:11+00:00

I have posted another question for the same project which was helpfully answered but

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T08:52:13+00:00Added an answer on June 10, 2026 at 8:52 am

    $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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have recently posted another question which straight away users pointed me in the
Following on from a question I posted yesterday about GUIs, I have another problem
So I just have posted a question about this code (which was answered): $(document).ready(Main);
I have posted a question on here previously asking similar advise, but this project
This sounds like a problem many others have posted about, but there's a nuance
I already posted a question regarding it, but at that time I haven't have
I posted another question which was resolved perfectly however I now need to apply
I have a question about this question . I posted a reply there but
I have posted a question about multilanguage database design here, [] What are best
I posted a question before but I am yet limited to mix the code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.