2014-03-27 90 views
0

比方說,你有兩個按鈕。其ID號:如何在PHP請求處理空間

bob 
bobs burgers 

您使用jquery的div元素來.load一個PHP頁面類似如下:

var teamSelect = $(this).attr('id'); 
$("#loadTables").load("php/leagueTable.php?team="+teamSelect); 

現在Bob將運行得很好,但開心漢堡店有一個空間,不工作。地址結束爲

"php/leagueTable.php?team=bobs" 

我無法控制的事實,將在按鈕Ids中有空格。這些按鈕是從數據庫生成的,這些名稱將始終有空格。我該如何處理這個問題?

+0

編碼uricomponent應該工作。 請參閱[http://stackoverflow.com/questions/6544564/url-encode-a-string-in-jquery-for-an-ajax-request][1] [1 ]:http://stackoverflow.com/questions/6544564/url-encode-a-string-in-jquery-for-an-ajax-request –

回答

0

您需要使用encodeURIComponent到url

編碼
$("#loadTables").load("php/leagueTable.php?team="+encodeURIComponent(teamSelect)); 

譯:encodeURIComponent()方法由 一個,兩個編碼的統一資源標識符 (URI),用特定的字符的每個實例分量,三個或四個表示編碼字符的UTF-8 的轉義序列( 字符只包含兩個「替代」字符組成的轉義序列),reference

+0

作品就這樣。謝謝。 – user2410532

+0

你可以在這裏檢查,http://jsfiddle.net/W72Vu/1/ – Adil

0

如果這些值不包含引號,然後用單引號括起來這些值。

像:

'bob' 
'bob burgers' 

這樣當它傳遞到URL這將是

php/leagueTable.php?team='bob' 
php/leagueTable.php?team='bob burgers' 

然後,你可以用trim()剝奪他們關閉之後。

0

w3.org

The value must be unique amongst all the IDs in the element's home 
subtree and must contain at least one character. The value must not 
contain any space characters. 

你可以找到更多here