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
-
This question is similar to: How do I pass variables and data from PHP to JavaScript?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem.dumbass– dumbass2024年08月01日 05:49:13 +00:00Commented Aug 1, 2024 at 5:49
4 Answers 4
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.
2 Comments
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.
5 Comments
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?
4 Comments
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).