Ajax php variables in javascript
I have the code below that previously worked fine:
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");
var phpstring = <?php $out = htmlspecialchars(json_encode($row2['ARTICLE_DESC']), ENT_QUOTES); echo("'$out'"); ?>;
child1.document.write(phpstring);
//child1.document.write("<?php echo htmlspecialchars(json_encode($row2['ARTICLE_DESC']), ENT_QUOTES); ?>");
child1.document.close();
}
The part that was written worked fine in the previus version as the javascript was replacing row2 ['ARTICLE_DESC'], a php variable with the content of the variable. This javascript file is included in the script tag in the php file and always worked fine. I recently changed something, but I'm not sure what the specific thing was, but I get these errors from firebug:
function makewindows(){
child1 = window.open ("about:blank");
child1.document.write("<br />
<b>Notice</b>: Undefined variable: row2 in <b>C:\Programme\EasyPHP 2.0b1\www\records4\fetchlayers.js</b> on line <b>57</b><br />
null");
child1.document.close();
}
unterminated string literal on line 57 and updateByQuery is undefined.
I have no idea why I am getting one of these errors and why updateByPk is not throwing an error. I am even more confused as to what article_Desc stands for and how. This happens to index.php, which has a link to call updateByQuery, which will load the section via ajax, which will have a link to updateByPk, which will display the final section, which will have a link to makewindows (), where article_Desc will refer to the corresponding $ pk
It all worked fine and I can't figure out why it is no longer the case.
Would it help me if I was inserting php files somewhere?
change.
I don't understand why this is happening, but tried to modify the function, so it takes a parameter.
function makewindows(html){
child1 = window.open ("about:blank");
child1.document.write(html);
child1.document.close();
}
combined with these two php snippets
$html = json_encode(htmlspecialchars($row2['ARTICLE_DESC']));
and
<a href='#' onclick='makewindows(/"".$html."/"); return false;'>Click for full description </a></p>
source to share
All indications are that the problem is with your PHP file. The notification you receive is PHP related, not JavaScript as you might expect.
<b>Notice</b>: Undefined variable: row2 in <b>C:\Programme\EasyPHP 2.0b1\www\records4\fetchlayers.js</b> on line <b>57</b><br />
null");
So the problem is here:
<?php $out = htmlspecialchars(json_encode($row2['ARTICLE_DESC']), ENT_QUOTES); echo("'$out'"); ?>;
The $ row2 array is undefined, so $ row2 ['ARTICLE_DESC'] does not exist. You have to check where it should come from because I couldn't find it in the code you provided.
source to share