Javascript problem - including php

I have the code pasted below which servers are the core of a small ajax application. This previously worked well, with makewindows actually displaying a popup containing the rsult of artcile_desc. I seem to have a bug in front of this function as now only the actual php code is output. This is not a problem with my server setup as I am an administrator and this has not changed.

I am getting the following errors with Firebug, but I'm 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(); 
}

      

Whatever I try to use the makewindows function just outputs the php code as html source and not the php code output. This worked fine before, and I'm not sure what I changed to lead to this behavior.

Now I have pasted all the code. The error is generated by a link that calls updateByQuery, which prevents makewindows from being divulged correctly .. I guess.

edit: php gets parsed when using 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

php result:

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 fixes the error

0


source to share


4 answers


First, whenever you encode something into a specific notation, you must convert the "special characters" before doing so if this 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'm still pretty confused, however, why are you even calling json_encode, the desc article must be a string, so:

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

      

Enough.

However this only sat on the page load, I assume this was your intention. If it doesn't work, look at the source and make sure it's in the original markup.

+1


source


I don't know what Firebug is complaining about, but I immediately see something else.

You cannot output PHP code from Javascript and wait for it to run. Javascript runs in the browser, PHP code must run on the server. Basically you provide the browser with a text file similar to PHP code, but the browser doesn't 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 this file on the server and the output will be in the window.

+2


source


Mmm, if JavaScript displays PHP code, it means the server no longer knows that something.php is supposed to run the PHP interpreter. You should double check the settings. And check that PHP is intact or something.

Also, the Firebug error you are showing is weird, it shows garbage. Perhaps you have installed a server (or script?) To send Gzipped data?

0


source


It looks like the PHP code is generating an error. (In fact, it even points 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 happening?

Also have a look at FirePHP - I haven't used it much, but it seems very useful and allows you to emit debug information from your PHP script in the Firebug console window (this is done via custom HTTP headers and the Firefox extension).

0


source







All Articles