views:

305

answers:

4

Hello,

I have the code pasted below, which servers as the core of a small ajax application. This was working fine previously, with makewindows actually displaying a popup containing the rsult of artcile_desc. I seem to have an error before that function however, as now only the actual php code is outputted. This is not a problem with my server setup, as I am the administrator and this has not changed.

I get the following errors with Firebug, but I am not sure what they mean.

unterminated string literal
onclick(click clientX=52, clientY=50)1GmRZ%2F...D9g%3D%3D (line 2)
[Break on this error] child1.document.write("<br />\n
1GmRZ%2F...D9g%3D%3D (line 2)
updateByQuery is not defined
onclick(click clientX=29, clientY=17)CLQWYjW1...WlQ%3D%3D (line 2)
[Break on this error] updateByQuery("Layer3", "Ed Hardy");

var xmlHttp
var layername
var url
function update(layer, url) {
    var xmlHttp=GetXmlHttpObject(); //you have this defined elsewhere

    if(xmlHttp==null) {
        alert("Your browser is not supported?");
    }

    xmlHttp.onreadystatechange = function() {
        if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
            document.getElementById(layer).innerHTML=xmlHttp.responseText;
        } else if (xmlHttp.readyState==1 || xmlHttp.readyState=="loading") {
            document.getElementById(layer).innerHTML="loading";
        }

       //etc
    }

    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
}


function updateByPk(layer, pk) {
   url = "get_auction.php?cmd=GetAuctionData&pk="+pk+"&sid="+Math.random();
   update(layer, url);
}


function updateByQuery(layer, query) {
   url = "get_records.php?cmd=GetRecordSet&query="+query+"&sid="+Math.random();
   update(layer, url);
}

function GetXmlHttpObject()
{
    var xmlHttp=null;
    try
    {
        xmlHttp=new XMLHttpRequest();
    }catch (e)
    {

        try
        {
                xmlHttp =new ActiveXObject("Microsoft.XMLHTTP");
        } 
        catch (e) {}

    }
return xmlHttp;
}
function makewindows(){
child1 = window.open ("about:blank");
child1.document.write("<?php echo htmlspecialchars(json_encode($row2['ARTICLE_DESC']), ENT_QUOTES); ?>");
child1.document.close(); 
}

Which whatever I try the makewindows function simply outputs the php code as the html source, and not the result of the php code. This was previously working fine, and I am not sure what I have changed to result in this behavior.

I have pasted all the code now. An error is generated by a link that calls updateByQuery, preventing makewindows from being parsed correctly..I think.

edit: the php is getting parsed when I use this code:

function makewindows(){
child1 = window.open ("about:blank");
child1.document.write("<?php echo htmlspecialchars(json_encode($row2['ARTICLE_DESC']), ENT_QUOTES); ?>");
child1.document.close(); 
}

But not the code above

the result of the php is this:

child1.document.write("<br />
58<b>Notice</b>: Undefined variable: row2 in <b>C:\Programme\EasyPHP 2.0b1\www\records4\fetchlayers.js</b> on line <b>57</b><br />
59null");

which cuases an error

A: 

Mmm, if the JavaScript displays PHP code, that means the server no longer knows that something.php must run the PHP interpreter. You should double check the settings. And verify PHP isn't corrupted or something.

Beside, the Firebug error you show is strange, it shows garbage. Perhaps you have set the server (or script?) to send Gzipped data?

PhiLho
No nothing like this. The entire site is php, and it all works fine. php variables are passed between the Javascript and PHP without problem. The only problem is getting the contents of article_Desc into the popup window.
Joshxtothe4
You didn't mention the error before. Where is the definition of row2? Take a look at what the php echo really outputs. Use CTRL-U in firefox to get a look at the page that was generated.
some
I only just managed to get firebug. The output is the same as before. I have index.php, which includes the javascript file. through the javascript file, get_records is loaded, which then loads get_auctions. that is all working fine, just not the popup window.
Joshxtothe4
It seems to be saying my function is not defined, and then breaking when trying to call that function
Joshxtothe4
A: 

Firstly, any time you encode anything to a particular notation, you should convert the 'special characters' before doing so, just in case it breaks the notation.

child1.document.write("<?php echo htmlspecialchars(json_encode($row2['ARTICLE_DESC']), ENT_QUOTES); ?>");

Should read:

child1.document.write("<?php echo json_encode(htmlspecialchars($row2['ARTICLE_DESC'], ENT_QUOTES)); ?>");

I am still rather confused however why you are even calling json_encode, article desc should be a string therefore:

child1.document.write("<?php echo htmlspecialchars($row2['ARTICLE_DESC']), ENT_QUOTES); ?>");

Would suffice.

However this would only get populated on page load, I'm guessing that was your intention. If it is not working, view source and make sure its in the source markup.

Jay
article_desc contains the code for a webpage, so it is all html
Joshxtothe4
Are you still having the problem or has it been resolved?
Jay
still having. I found an earlier version, where the php in the javascript is getting passed perfectly. It is something I have changed that has broken it, and I do not know what it is
Joshxtothe4
"any time you encode anything to a particular notation, you should convert the 'special characters'" ???? JSON handles everything correctly. He's just trying to show the JSON notation verbatim.
Jason S
Jason, he was calling html entities AFTER json encoding it... please read the code! This would result in html entities that have already been encoded in json encode, to then become html entities. Sure it may NOT be an issue with json encoding, but in general practice it is bad to do this!
Jay
json_encode doesn't encode HTML entities. The only chars escaped in JSON, besides whitespace/nonprintable/unicode are " / \. (see www.json.org) If the strings in question have HTML tags like <b>, they will not be escaped using JSON. The reason to use htmlspecialchars is to show something verbatim.
Jason S
+2  A: 

I do not know what Firebug complains about, but I instantly see something else.

You cannot output PHP code from Javascript and expect it to run. The Javascript is executed in the browser, the PHP code should be executed on the server. Basically you are giving the browser a text file that looks like PHP code, but the browser does not know what to do with it.

If you want to execute PHP code, put it in a file on your web server. Point the browser window to that file on the server, and the output will be in the window.

Vegard Larsen
I want javascript to use the contents of a php variable, not run php code as such
Joshxtothe4
Then you have the PHP code on the SERVER, not in JavaScript. Have it echo out the value, and pick it up from Javascript.
Vegard Larsen
A: 

It looks like the PHP code is producing an error. (In fact it's even pointing you to fetchlayers.js on line 57) Perhaps you should wrap it with a try/catch block to handle errors, or at least show what errors are occurring?

Also take a look at FirePHP -- I haven't used it much, but it seems very useful & lets you emit debug information from your PHP script into Firebug's console window (this is done via custom http headers and a Firefox extension).

Jason S
The problem is that I can not tell why the php code is writing some html to document.write as well as an error, that is not caused by the php code
Joshxtothe4
weird -- maybe Firebug is interacting strangely with the php's Javascript output... The only red flag that catches my attention is your use of PHP code within quotes. Something is strange here. I wonder if it would be easier to debug this way:
Jason S
var phpstring = <?php $out = htmlspecialchars(json_encode($row2['ARTICLE_DESC']), ENT_QUOTES); echo("'$out'"); ?>;child1.document.write(phpstring);
Jason S
Hmm, it produces: var phpstring = <br />58<b>Notice</b>: Undefined variable: row2 in <b>C:\Programme\EasyPHP 2.0b1\www\records3\ajaxlib.js</b> on line <b>57</b><br />59'null';60child1.document.write(phpstring);61//child1.document.write("<br />62<b>Notice</b>:
Joshxtothe4
Undefined variable: row2 in <b>C:\Programme\EasyPHP 2.0b1\www\records3\ajaxlib.js</b> on line <b>59</b><br />63null");I don't understand where the html code is coming from if it is not parsing the php
Joshxtothe4
Aha: that's a clue. Your "Hmm, it produces..." comment has 'null' (what php() is intentionally echoing) but it's preceded by the HTML notice... perhaps you're not handling the case properly where $row2 is undefined?
Jason S