[英]ajax php variables in javascript
I have the below code, which was previously working 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 is commeneted out was working fine in a previus version, in that javascript was replacing row2['ARTICLE_DESC'], a php variable with the contents of the variable. 在上一个版本中,被commenet的部分可以正常工作,因为javascript用其内容替换了PHP变量row2 ['ARTICLE_DESC']。 This javascript file is included from a script tag in a php file, and always worked fine.
此javascript文件包含在php文件中的脚本标签中,并且始终可以正常工作。 I have changed something recently however, I am not sure what the specific thing was, butnow I get these errors, from firebug:
我最近做了一些更改,但是我不确定具体是什么,但是现在我从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 the updateByQuery is not defined. 第57行上的未终止字符串文字,并且未定义updateByQuery。
I have no idea why I get either of those errors, and why updateByPk does not throw an error. 我不知道为什么会遇到这些错误,以及为什么updateByPk不会引发错误。 I am even more confused as to what article_Desc is being expanded to and how.
对于article_Desc扩展到什么以及如何扩展,我更加困惑。 This happens on the index.php, which has a link to call updateByQuery, which would load a section via ajax which would have a link to updateByPk, which would display the final section, which would have a link to makewindows(), where article_Desc would relate to the relevant $pk
这发生在index.php上,该链接具有一个调用updateByQuery的链接,该链接将通过ajax加载一个部分,而该ajax则具有一个指向updateByPk的链接,后者将显示最后一个部分,该链接将具有makewindows()的链接,其中article_Desc将与相关的$ pk相关
This was all working fine, and I can not find out why it no longer is. 一切都很好,我不知道为什么不再如此。
would it help if I were to paste the php files somewhere? 如果我将php文件粘贴到某处会有所帮助吗?
edit. 编辑。
i do not understand why this is happening, but have tried to modified the function so it takes a paramter. 我不明白为什么会这样,但是已经尝试修改该功能,因此需要一个参数。
function makewindows(html){
child1 = window.open ("about:blank");
child1.document.write(html);
child1.document.close();
}
in conjunction with thse two snippets of php 结合这两个PHP片段
$html = json_encode(htmlspecialchars($row2['ARTICLE_DESC']));
and 和
<a href='#' onclick='makewindows(/"".$html."/"); return false;'>Click for full description </a></p>
Everything indicates the problem is in your PHP file. 一切都表明问题出在您的PHP文件中。 The notice you are getting is from PHP and not from JavaScript as you may be assuming.
您得到的通知是从PHP而不是您可能假设的JavaScript。
<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 in here: 所以问题出在这里:
<?php $out = htmlspecialchars(json_encode($row2['ARTICLE_DESC']), ENT_QUOTES); echo("'$out'"); ?>;
The $row2 array is not defined, so $row2['ARTICLE_DESC'] doesn't exist. $ row2数组未定义,因此$ row2 ['ARTICLE_DESC']不存在。 You should verify from where it should come from because I couldn't find it in the code you provided.
您应该验证它的来源,因为我在您提供的代码中找不到它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.