1

I want to form a string in my php server code as xml, and then send it to javascript so that ajax.responseXML can navigate through it and do things with the data. I haven't been able to find exactly what I need to accomplish this and have tried a few different methods. Here's the most recent thing I've tried.

<?php
   header("Content-type: text/xml");
   $xmlstring = "<?xml version'1.0' encoding='UTF-8'>";
   $xmlstring = $xmlstring . "<book name='$name' author='$author'>";
   foreach($rankings as $entry)
   {
      $xmlstring = $xmlstring . "<rank>$entry</rank>";
   }
   echo $xmlstring;
?>

I know the data is getting there because if I echo it as a string and open it directly, the numbers I need are getting printed. I'm using Ajax.Request to open the php file with certain parameters, and when it reaches the onSuccess function, ajax.responseXML is null. This is my first time dealing with xml so I could be doing something stupid.

function that makes the call:

function findRankings(author, name)
{
    new Ajax.Request("server_code.php",
                {
                      method: "get",
                      paramters: {"type": "rank", "name": name, "author": author},
                      onSuccess: makeGraph,
                      onFailure: ajaxFailed
                });
}

function makeGraph(ajax)
{
      alert(ajax.responseXML); // testing that it made it
      .....// do stuff with the response
}

EDIT: I added the header and made it echo just the string. I also added the ajax functions. I keep getting null though. :(

3
  • Might want to add header('Content-Type: text/xml'); Not sure if this will help ajax.responseXML recognize it. Commented Apr 16, 2015 at 18:17
  • Found this too: stackoverflow.com/questions/2124924/… Could you add your script that is making the Ajax call? Commented Apr 16, 2015 at 18:19
  • I added the header and the scripts as well. The variables in my php file get set before the part that I posted. I know those are right though so that shouldn't be the issue. Commented Apr 16, 2015 at 18:40

2 Answers 2

2

As Dustin said, you need to echo $xmlstring; instead and add header('Content-Type: text/xml');

But you also have a couple of errors in your XML declaration. You're missing a = and a ?:

$xmlstring = "<?xml version='1.0' encoding='UTF-8'?>";

I'd recommend using an XML validator in future.

Sign up to request clarification or add additional context in comments.

1 Comment

i had the "=" in my actual code. I didn't have the "?" though. I added it and no longer am I getting null. Thank you for catching that. I'll definitely be using the validator from now on.
1

You used simplexml_load_string, which converts your XML string to an object.

Just echo your $xmlstring

For clean coding you should insert header('Content-Type: text/xml'); as @Twisty mentioned.

3 Comments

I get the same problem when I just echo $xmlstring. I'll try adding the header that you guys mentioned.
When this doesn't help, please add your javascript code and the error message.
sorry, i meant to do that. I added all the changes that I made.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.