Can’t send content-type: text/xml header from PHP at the same time getting the data from MYSQL – Education Career Blog

i hope you can cast some light on my problem. I need to do an AJAX / PHP / MYSQL application to display posts and stuff on the page i’m writing.

I only discovered how to do some simple stuff in PHP after taking some mushrooms but that was years ago and now i don’t have mushrooms and i’m just stuck!

So here’s the problem:

i think i need to send a proper “xml” file through php so the ajax part can take it but: when i try to put the header on top of the php it displays this error:

” Extra content at the end of the document “

When i looked at some tutorials people were using the “header” fearlesly to do such stuff as i want to do and no comments suggested that it didn’t work. so why it doesn’t work on my local server?

I’m running:

Apache 2.2.11
PHP 5.3.0

It also doesn’t work on a remote server (PHP 5.3.0) :/

I read all the stuff i could find till 5am and decided to ask you for help for the first time 🙂

Thank you!

header('content-type: application/xhtml+xml; charset=utf-8'); 


$wypluj="<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>";    

$bazinga_blob = new bazingablob;

if (!$bazinga_blob->connect_to_database()) 

            echo $wypluj;






if ($bb_result) //udalo sie cos znalezc w bazie wedlug kryteriow














                    foreach ($bb_result as $item) 







echo $wypluj;


The error comes from your browser and indicates that your XML is malformed.

Setting the application/xhtml+xml header tells the browser to process the document as serious XML. XML needs to be “well-formed”, i.e. it must not contain any syntax errors. Apparently you do have a syntax error on line 1 at column 73, which makes the browser abort the attempt to process the document.

For this reason it’s a pain to hand-code XML, you should really look into a library that takes care of the well-formedness for you, like PHP’s own XMLWriter.


Have you validated your XML?


I’m honestly not sure what you’re trying to do with the header, it’s not any Ajax method I’ve ever been taught. The header method you’re doing looks just a few lines short of outputting the XML to a download prompt.

Here’s my favorite way to do AJAX. Simple, understandable, and quick.

  1. Include Jquery.
  2. Setup your data–whether by form with a Serialize (gets form data into a Javascript Variable) or by just setting some variables as it seems you’re doing above.
  3. Send via Jquery Ajax to a separate processing page. The page will receive the data you setup as a $_REQUEST variable, with the method depending on whether you defined it as a POST or not (defaults to a GET)
  4. The processing page –does– stuff with the REQUEST data and may or may not respond back to the page. This is where you can do stuff like update divs, alert that it worked, etc.

Here’s a great tutorial. Focus on the code under “Hello Ajax, Meet Jquery”

If you get yourself any more of those mushrooms, a PHP familiar way to do AJAX is with XAJAX. It allows you to do asynchronous calls to PHP functions. Be aware, though, that the forums are not the most english-friendly and documentation is a bit cryptic.

Leave a Comment