【发布时间】:2013-08-09 05:08:57
【问题描述】:
我的 thePhpFile.php 文件中有以下代码来处理 Ajax 调用:
<?php
require_once('usefulStuff.php'); // stuff used throughout the code
if (isset($_GET['zer']))
{
$bFound = false;
if(! $bFound)
{
echo "notfound";
return;
}
else
{
echo "found";
return;
}
}
?>
这是处理 responseText 的内联 onreadystate 函数 (javascript):
var theResponseText = "rText";
var zer = "testing";
xmlhttp.onreadystatechange = function()
{
if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
theResponseText = xmlhttp.responseText;
alert("responseText is >>>" + theResponseText + "<<< that.");
if( theResponseText == 'notfound')
{
alert("sorry, nothing found.")
}
}
}
var ajaxText = "thePhpFile.php?zer=" + zer;
xmlhttp.open("GET", ajaxText, false);
xmlhttp.send();
如果我真的在我的有用Stuff.php 包含文件中添加换行符或其他任何内容 - 我将它添加到有用Stuff.php 的底部,在 ? > 结束标记——上面的以下代码行,一个回显语句,不遗余力地定位和抓取那些额外的换行符等,并在我的 responseText 中返回它们:
echo "notfound";
编写了一个编译器并处理了 BNF 语法,我不明白为什么 php 中的 echo 语句设置为“echo”,而不是紧跟在“echo”之后直到第一个分号 ';'在解析过程中遇到。
我知道我可以使用 trim() 来撤消空格,但我的问题是,我想强制上面的 echo 语句按照上面的 echo 语法所建议的方式进行操作 应该。如果上面的回显语句有充分的理由在我的包含文件中寻找无关的空白以返回我的“未找到”文本,我不知道那是什么原因,但我想禁用这种意外行为.
我希望我的代码行 echo "notfound"; 做到这一点,仅此而已 - 只是回显单词 notfound 并在在“未找到”文本之后立即遇到分号。
如何将 echo 行为限制为仅回显紧随单词 echo 的内容,并在到达分号时停止回显?
顺便说一句,在尝试使用 ?> 终止标记之外的有用Stuff.php 文件的内容时,我在该文件的末尾添加了以下内容:
// now ending the php code in usefuleStuff.php:
?>
<noscript>
<meta http-equiv="refresh" content="0;
URL=http://mywebsite.com/noscript.html"/>
</noscript>
代码行 echo "notfound"; -- 当我检索我的 responseText -- 响应文本还包含所有三个 noscript 代码行,以及任何额外的空白,除了我的“未找到”。
所以 php 中的“回声”是垃圾收集我在包含的有用Stuff.php 文件末尾放置的任何内容。
如何限制 echo 的行为以执行代码行 echo "notfound"; 让您相信它会做的事情,即只回显单词 "notfound" ?
【问题讨论】:
-
常见的,只是裁剪你的问题..只是发布你的疑问的相关部分
-
删除 echo 语句并再次运行会发生什么?
-
我看到你们两个人说的我可能会说的,我包含的有用Stuff.php 文件的内容,该文件位于关闭 ?> 标记之外的文件末尾——即使没有回显声明,我的 responseText 仍然包含数据,我的包含文件中的关闭 ?> 标记之外的东西——谢谢,我不知道这是怎么发生的,我很感激你的解释。
标签: php javascript ajax echo