2010-12-12 55 views
2

我正在使用一些jquery/php通過.load()函數調用時更新一個數字。Php從jquery.load回顯空白

所以可以說我有

<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草稿'上方的空白?是什麼造成的?

謝謝!

+2

你可能有白人在開始'<?'之前加入countItems.php內部的節奏。 – 2010-12-12 03:30:59

+0

我已將countItems.php的完整內容添加到原始文章中。 <?坐在頁面的第一行。 – Andelas 2010-12-12 03:34:24

+0

謝謝。實際上,我在包含的連接文件中發現了問題。它有空白,我從未注意到:O再次感謝! – Andelas 2010-12-12 04:16:57

回答

2

如果你願意,你可以在JavaScript中創建一個修剪功能,但我認爲Pekka可能是正確的。最好不要返回空格。

您也可以修改您的響應,以返回一個帶有該文本的json對象作爲值。在聲明JSON對象之前是否有空格並不重要。

1

不是很優雅,但如果您無權訪問服務器腳本,則可以在客戶端上修剪結果。例如:

  • 有實際上是腳本
  • UTF8與BOM是的解釋部分前的空白:

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

    順便說一句,從可能發生在服務器端腳本結果空白使用

  • 服務器軟件是越野車(使用Apache,沒有模塊,如果沒有上述作品)