views:

2879

answers:

5

First off, this isn't exactly the ideal way of setting up a page, however there's a need to distribute a script as 1 file.

I have a php script at the top of an otherwise xhtml document with javascript, and under certain conditions use XHR to send a query string to the page itself. The php at the top then activates, and stores the passed content as a session, and then kills itself (exit()). The XHR is async and is never checked to see if it returns content.

However in Firefox 3, the error console throws an error 'no element found' every time the XHR request gets sent. Also, if I use an exit such as exit('Done'), Firefox throws a syntax error of (Done) as if it inserts it into the visible DOM. This doesn't seem to happen in opera.

Is there a better way to store a session from an already generated xhtml page? Obviously I could XHR to another page, but I would prefer to keep it all on one script. Does firefox treat XHR requests to self as updates to the DOM? I don't know why it's sending this error.


Update As I said, firefox only thows the error when the XHR request is made. The page is valid XHTML and works perfectly, without error unless the XHR request is made to the page itself.

I was wondering why it was sending the error because it really doesn't return anything.

Here's a javascript snippet that makes a ajax request from an object. It creates a XHR object, without a callback function, and posts the information. It works properly when not referencing the same page.

 var saveState = { saveContent: function(updateActiveMenu) {
 var sendState = new ajaxObject(gV.url);
 if (!updateActiveMenu) {
  var storageContainer = document.getElementById("StorageContainer").innerHTML;
  var menu = document.getElementById("Nav").innerHTML;
  sendState.update("Containerstring="+urlencode(storageContainer)+"&Nav="+urlencode(menu)+"&Active="+gV.activeMenuItem, 'POST', true); } }, }

And the php does this

    if (isset($_REQUEST['Containerstring']) && isset($_REQUEST['Nav']) && isset($_REQUEST['Active'])) {
$_SESSION['Containerarray'] = (saveContainer(regulateEscapes(urldecode($_REQUEST['Containerstring']))));
$_SESSION['Navarray'] = (saveNav(regulateEscapes(urldecode($_REQUEST['Nav']))));
$_SESSION['Active'] = $_REQUEST['Active'];
exit('Done'); }

I'm also aware I shouldn't be using innerHTML but that's another story


The error is this Error: no element found Source File: http://localhost/ajax.php?1244648094055 Line: 1

Take note that the error, while on the php page I'm using, references a query string that is never called.

A: 

if you really have PHP code at the TOP of the file then it may not be valid xhtml and maybe all bets are off. Do you have your doctype on the first line of output? Are you returning the correct content-type?

SpliFF
huh? how's that related to xhr?
SilentGhost
because if the document is not well-formed XHTML/XML and you're trying to act on its elements you would have problems. I don't entirely follow the logic of what you're attempting so I can't say more without some source code.
SpliFF
The logic is that this page will be widely distributed, and while not exactly efficient, this way all its workings will be in one source, copy paste and save, rather than worrying about users downloading a zip and using each file properly.
Ian Elliott
I'm not disputing that. What I'm saying is that if the DOCTYPE isn't on the first line of output or the request contains any invalid XML you may see side-effects if you or the browser tries to process the data as a document. Remember it's an "XML" HttpRequest and even if you're throwing away the result that doesn't mean the browser isn't trying to turn it into a XML DOM. I don't use XHR enough to confirm this is an issue but hopefully you understand my reasoning at least.
SpliFF
+6  A: 

Firefox is expecting to get something it can parse as XML back, and throwing an XML parsing error when it gets an empty response.

Before your PHP calls "exit()", use

header('Content-Type: text/plain');

and Firefox will not try to parse the response as XML, and there should be no error.

NickFitz
Never mind, I used the "HTTP/1.0 200" header and assumed this would do the same, I was definitely mistaken ;) This worked, thanks a lot.
Ian Elliott
A: 

You should have your php return a valid HTTP response, because Firefox will try to read it even if you don't do anything with it. So return at least a valid header

header("HTTP/1.0 200");
exit();
Alsciende
This solution didn't work, however the 'Content-Type' header did.
Ian Elliott
A: 

I am a beginner with php and ajax and I am still having this issue and viewing the error from FF, Error Console, it seems to point to the ?> of the file.

I have also tried header('Content-Type: text/plain');

Please help .. Thanks.

FYI, this is my simple index.html:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;
<html>
  <head>
    <script type='text/javascript' src='ajax.js'></script>
    <title>PHP AJAX Example</title>
  </head>
  <body>
    <input type='button' 
      onclick='MakeRequest();' value='Use AJAX!!!!'/>
    <div id='ResponseDiv'> This is a div to hold the response.
    </div>
  </body>
</html>

ajax.js calls: xmlHttp.open("GET", "ajax.php?a="+Math.random(), true);

and ajax.php is:

<?php header("HTTP/1.0 200");
echo "This is a php response to your request!!!!!!";
?>
A: 

Hi, I am also getting "no element found"

please help

Amol Agarwal