来自jquery.load的Php echo空格

时间:2010-12-12 03:28:49

标签: php jquery load

我通过.load()函数调用时使用了一些jquery / php来更新数字。

所以我说我有

<span id='draftCount'>1 Draft</span>

我打电话给jquery更新它

$('#draftCount').load('countItems.php?cid=draftCount');

现在是countItems.php的内部

<?
    include("connect.php");
mysql_select_db ("news");

$countWhat = $_GET['cid'];

if($countWhat   == 'binCount') {
    $pullBin = mysql_query("SELECT * FROM bin");
    $count = mysql_num_rows($pullBin);
    echo '$count';
}

if($countWhat   == 'draftCount') {
    $pullBin = mysql_query("SELECT * FROM   `main`  WHERE   `active` < '2'");
    $count = mysql_num_rows($pullBin);
    if ($count ==   '1') $drafts = 'Draft';
    if ($count !=   '1') $drafts = 'Drafts';
    $count = "$count $drafts";
    echo "$count";
}

?>

结果是

<span id="draftCount">





 4 Drafts</span>

如何摆脱'4 Drafts'以上的所有空白?是什么导致了它?

谢谢!

2 个答案:

答案 0 :(得分:2)

如果你愿意,你可以在javascript中创建一个修剪函数,但我认为Pekka可能是正确的。最好不要返回空白。

您还可以修改响应以返回带有该文本作为值的json对象。在声明JSON对象之前是否有空格并不重要。

答案 1 :(得分:1)

不是很优雅,但如果您无法访问服务器脚本,则可以在客户端上修剪结果。例如:

$.get("countItems.php?cid=draftCount", function(data) {
    $('#draftCount').html($.trim(data));
});

顺便说一句,服务器端脚本结果的空格可能来自:

  • 在剧本的解释部分
  • 之前实际上有一个空格
  • 使用带有BOM的utf8
  • 服务器软件有问题(如果以上都不起作用,则使用没有模块的apache)