Topic: NEWBIE BUG NOTICE: WHITESPACE IN YOUR RETURNED XML

Yes, I just d/l this library tonight so this is something intresting I found.  I would love it if someone would explain it to me to tell me why this happens.  So can someone please expain the following behavior please?

Sorry if this has been covered but I didn't find it searching google so I thought I would let you fellow n00bs know about this issue that I found.  Ok, so I was playing around and using an include file to get my database connection.  What I have found might or might not shock you all.   If you have split php tags, in other words, open tags  close tags, html, open tag close tags then there cannot be any whitespace between the closing of a php tag and the next open tag. I guess what it amounts to is that there can be nothing written on the page prior to the call to

$xajax->ProcessRequests();

Incorrect code will cause whitespace error!!!

<?php 
  // I am doing some php here
?>
<!-- This is some html.  Notice that even a blank space is considered output-->
<?php
 //I am doing some more php here too.
 $xajax->ProcessRequests();
?>

To fix this I found that I had to remove any white spaces between the closing php tag and the opening of another php tag.  It is important to note that ANY HTML prior to the calling of ProcessRequests(); will result in a fatal error for your xajax application.

Correct code will NOT cause whitespace error.  Notice no ouput until after ProcessRequests() is called.

<?php 
  // I am doing some php here
  // note below that there is not comment or space (Hence not outputed html)
?>
<?php
 //I am doing some more php here too.
 $xajax->ProcessRequests();
?>

Whelp, this is what I found.  I hope this helps someone else that beats their head against the wall like me.  Any additional info on this topic would be wonderful. 


Thanks
Doopsterus

Re: NEWBIE BUG NOTICE: WHITESPACE IN YOUR RETURNED XML

Make sure you have no whitespace before <?php

Re: NEWBIE BUG NOTICE: WHITESPACE IN YOUR RETURNED XML

You may also find that turning on output buffering will help get rid of these errors.
You can do this from php.ini using:

output_buffering = On

You can do this in your code by using:

ob_start();

before any output

Re: NEWBIE BUG NOTICE: WHITESPACE IN YOUR RETURNED XML

i've also realise this whitespace error yesterday... similar but a little different.

<?php
include database_connect.php;

function some_function()
{
    // some query to database

     $objResponse->getxml();
}


// xajax stuff...
$xajax->ProcessRequests();

// connect database codes

// database query

?>


when i click submit to run the xajax registered function, i get the whitespace error.
it is probably causing whitespace due to the error message of no database connection.

then i move the connect database code before the $xajax->ProcessRequests(); and it all went fine.

Re: NEWBIE BUG NOTICE: WHITESPACE IN YOUR RETURNED XML

Before your  <?php statement in your included file,  look for any spaces or carriage returns, and after your ?> look for carriage returns or spaces.  Once I removed them my file loaded fine.

Re: NEWBIE BUG NOTICE: WHITESPACE IN YOUR RETURNED XML

I've found that I sometimes get white-space error messages if my sql queries are bad (syntactically).

I don't know if this is what you have problems with, though I see that you are connectiong to db's

Re: NEWBIE BUG NOTICE: WHITESPACE IN YOUR RETURNED XML

Hi...!!!

May be if you had written "<? php" somewhere in script that's y you are getting this type of error.

Thanks.

Re: NEWBIE BUG NOTICE: WHITESPACE IN YOUR RETURNED XML

Is there a patch one could apply to ->ProcessRequests() to clean this at that level?

Re: NEWBIE BUG NOTICE: WHITESPACE IN YOUR RETURNED XML

BigBrownChunx wrote:

You may also find that turning on output buffering will help get rid of these errors.
You can do this from php.ini using:

output_buffering = On

You can do this in your code by using:

ob_start();

before any output

when i have output buffer turned on, the loading seems to take forever and will not come out with the contents.
by the way, why do i not see errors sometimes even though things are not behaving right? it is quite difficult to debug without the errors showing.

10 (edited by sylaryn 2010-03-24 3:58:56 AM)

Re: NEWBIE BUG NOTICE: WHITESPACE IN YOUR RETURNED XML

xEnOn wrote:

when i have output buffer turned on, the loading seems to take forever and will not come out with the contents.
by the way, why do i not see errors sometimes even though things are not behaving right? it is quite difficult to debug without the errors showing.

When you've collected all your output and are ready to send it to the browser, use:

ob_end_flush();

This will close the output buffer and flush the contents to the screen for display.

Edit: WOW! I just replied to a 3-year old post!! tongue

Re: NEWBIE BUG NOTICE: WHITESPACE IN YOUR RETURNED XML

Hello, I see that this is a very old thread stuck to the top of the forum but I can't really tell if it's resolved.  I would like to use XAJAX in a larger application that I wrote where the xajax calls would happen way down in files that aren't the first thing to load and other PHP and HTML must load before I get to the ajax stuff.  I'm finding that I can't make anything work because of this need to have $myAjaxObj->processRequest(); within the very first PHP block on the page. 

I read this post when I first started seeing a problem and it still took me an hour to figure out that the issue really was just that $myAjaxObj->processRequest(); was too far down in my page.   I haven't encountered php functions before that are sensitive to their placement relative to whitespace or HTML, so is there a workaround to this that would help me use XAJAX in my application?

Otherwise, it looks like a really nice library!   Thanks, -Bob

Re: NEWBIE BUG NOTICE: WHITESPACE IN YOUR RETURNED XML

Hi Bob,

This is a really old thread but if someone is still having problems..

Any whitespace before the opening php tag or after the closing tag will be immediately output and hose the whole thing.

I have gotten bit several times having a space or newline before the opening tag.

One of the best things you can do is not use a closing tag so php takes care of anything trailing.

Make sure no leading or trailing in included files as well.

The output buffering also seems to take care of the problem. I always use it at the top of the page but never use the ob_end_flush function. php will take care of it at the end of the php. I like that the page processing is not interrupted with i/o..

Look at the code in an editor that can show non-printing characters.

As for the placement of the processRequest call, It can be placed most anyplace. Any code before and after the call is processed during the page load and only the code before it is processed on an xajax call. I usually place my call near the end. Do you have some code outputting before the call?

Hope this helps.

If you still have a problem, post the code.

Ed

If you ever stop learning you may as well dig a hole, crawl in and pull the top over yourself.

13 (edited by diginized 2012-08-12 1:10:18 AM)

Re: NEWBIE BUG NOTICE: WHITESPACE IN YOUR RETURNED XML

Hi Ed,

Thank you for your reply.  Output buffering is not something I've messed with before, but I tried to turn it on by placing ob_start() at the top of my page.  This didn't change anything, it still doesn't work unless ->processRequest() appears within the very first <?php bracket.  I'm including my code below, the <?php that you see there is the absolute start of this file.

I appreciate you trying to help, and any insight you can provide.

There are comments in the code explaining exactly what I see at different places as I move processRequest() around the file.

Also, for my general education, because I don't pretend to be a very skilled programmer, could you help me understand why the xajax library actually doesn't like this whitespace at the start?  I haven't seen this before in any other packages I've tried using.

<?php 
session_start(); 
ob_start();
require_once("include/xajax_0.5/xajax_core/xajaxAIO.inc.php");

$myAjaxObj = new xajax();
  $myAjaxObj->configure('javascript URI', 'include/xajax_0.5/');
  $myAjaxObj->registerFunction("printForm");

function printForm($firstname){
  $response = new xajaxResponse();
  $response->assign("answer","innerHTML",$firstname);
  return $response ;

}

//If I place $myAjaxObj->processRequest();  here, then the page works.

?>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<?php
$myAjaxObj->processRequest();  //When I place this here, it does not work. 
        /*I placed the processRequest() function here because this is my test page,
        but my real application has many more require_once() of other php files 
        before it gets to the page I'd like to add xajax to.  So, it would not be a fair test to 
        include processRequest at the top of this page (where it seems to work) 
        This is what I'd like help with.        
        
        When placed here, the Chrome console outputs "Uncaught SyntaxError: Unexpected token )"
        whenever an xajax call is made, and it fails.
        */ 

//phpinfo();  //says output buffer is set to 4GB
?>
<html>
<head>
<title>Page title</title>
</head>
<body>

<form action="#" onsubmit="return false;" method="post">
  <input type="text" size="30" name="firstname" id="firstname" />
  <a href="javascript:void()" onclick="xajax_printForm(xajax.$('firstname').value);">Submit Changes</a> 
  <input type="button" onclick="xajax_printForm(10);" id="btnReset" value="the answer" />
</form>

<div id="answer" style="width:300; border:1px solid green;"><br /><br /></div>

</body>
</html>

<?php $myAjaxObj->printJavascript();  ?>

Thanks again!

Re: NEWBIE BUG NOTICE: WHITESPACE IN YOUR RETURNED XML

OK here's the deal...

Whan you make an xajax call the XHR object is sent to the server and the xajax javascript immediatley begins to soak up anything returned. It is expecting valid XML. If you have ANY output before the returned xml, the javascript gets messed up trying to interpret the response.

Here is your code with different placements of processRequest:

<?php 
session_start(); 
ob_start();
require_once("xajax/xajax_core/xajax.inc.php");

$myAjaxObj = new xajax();
  $myAjaxObj->configure('javascript URI', 'xajax/');
  $myAjaxObj->registerFunction("printForm");
//  $myAjaxObj->processRequest(); //This works
function printForm($firstname){
  $response = new xajaxResponse();
  $response->assign("answer","innerHTML",$firstname);
  return $response ;
}
$myAjaxObj->configure('debug',true);
//$myAjaxObj->processRequest(); //This works
?>
<?php
//$myAjaxObj->processRequest(); //This works. Nothing has been output yet...
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<?php
$myAjaxObj->processRequest(); //This does not work The doctype was output to the browser. See the debug info
?>
<html>
<head>
<title>Page title</title>
<?php $myAjaxObj->printJavascript();  ?>
</head>
<body>

<form action="#" onsubmit="return false;" method="post">
  <input type="text" size="30" name="firstname" id="firstname" />
  <a href="#" onclick="xajax_printForm(xajax.$('firstname').value);">Submit Changes</a> 
  <input type="button" onclick="xajax_printForm(10);" id="btnReset" value="the answer" />
</form>

<div id="answer" style="width:300; border:1px solid green;"><br /><br /></div>

</body>
</html>

You should be able to do any number of require/include calls so long as none of the included/required files does not write to the browser.

If you ever stop learning you may as well dig a hole, crawl in and pull the top over yourself.

Re: NEWBIE BUG NOTICE: WHITESPACE IN YOUR RETURNED XML

Thanks Ed,

I played around with what you're talking about and found that i'm still going to get errors if there are any breaks between php tags, even if output hasn't gone out toe the browser yet.  Does this sound correct?

Using the code you pasted above.   This works:

$myAjaxObj->configure('debug',true);
//$myAjaxObj->processRequest(); //This works
?>
<?php
$myAjaxObj->processRequest(); //This works. Nothing has been output yet...
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<?php

But this doesn't:  (the only difference being that line break on the 4th line below)

$myAjaxObj->configure('debug',true);
//$myAjaxObj->processRequest(); //This works
?>

<?php
$myAjaxObj->processRequest(); //This works. Nothing has been output yet...
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<?php

I suppose what this is suggesting to me is that as soon as the php processor recognizes a break that isn't php (any character not between <?php and ?> ) it starts outputting to the browser, and that's what throws off the xajax functionality.  Correct?

If so, in order to make it work, I guess I'd have to go tidy up any code in my require() files that has breaks between php tags..

I think I'm starting to get it, please correct me if I'm wrong.  Thanks again for your time on this.

Re: NEWBIE BUG NOTICE: WHITESPACE IN YOUR RETURNED XML

I think you've got it.

When the PHP ends the http processing begins.

If you ever stop learning you may as well dig a hole, crawl in and pull the top over yourself.