A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2008 yearjavascript problem - including php - page 1
User InfoPosts
javascript problem - including php#1
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

posted date: 2008-12-16 03:50:00


Re: javascript problem - including php#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2008-12-16 03:50:01


Re: javascript problem - including php#3
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?

posted date: 2008-12-16 04:15:00


Re: javascript problem - including php#4
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.

posted date: 2008-12-16 04:57:00


Re: javascript problem - including php#5
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.

posted date: 2008-12-16 05:22:00


Re: javascript problem - including php#6
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.

posted date: 2008-12-16 05:28:00


Re: javascript problem - including php#7
article_desc contains the code for a webpage, so it is all html

posted date: 2008-12-16 05:55:00


Re: javascript problem - including php#8
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

posted date: 2008-12-16 06:20:00


Re: javascript problem - including php#9
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).

posted date: 2008-12-16 06:23:00


Re: javascript problem - including php#10
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.

posted date: 2008-12-16 06:47:00


Re: javascript problem - including php#11
var phpstring = <?php $out = htmlspecialchars(json_encode($row2['ARTICLE_DESC']), ENT_QUOTES); echo("'$out'"); ?>; child1.document.write(phpstring);

posted date: 2008-12-16 06:49:00


Re: javascript problem - including php#12
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!

posted date: 2008-12-16 07:45:00


select page: « 1 2 »
Copyright ©2008-2017 www.momige.com, all rights reserved.