2012-07-16 62 views
0

我有一個工作代碼,它允許用戶將url插入數據庫,並顯示數據庫的內容。Addinto MySQL與AJAX和PHP

問題進出口運行到時我使用AJAX來addinto數據庫是,如果一個URL的字符「&」它減少URL中的字符,例如前右:

1. http://www.arisroyo.com/wp-content/uploads/2012/07/php.png 
2. http://www.arisroyo.com/wp-content/uploads/2012/07/test&123.png 

當添加URL 1數據庫我沒有問題,但添加URL 2時到數據庫中,我得到這個添加到數據庫中:

http://www.arisroyo.com/wp-content/uploads/2012/07/test 

它忽略了「&」的牌子後,任何東西。問題是我的所有網址都會使用「&」標誌。

這是使用AJAX的得到來自網站用戶的URL代碼IM:

var nocache = 0; 
function insert() { 
// Optional: Show a waiting message in the layer with ID login_response 
document.getElementById('insert_response').innerHTML = "Just a second..." 
// Required: verify that all fileds is not empty. Use encodeURI() to solve some issues about character encoding. 
var site_url= encodeURI(document.getElementById('site_url').value); 

// Set te random number to add to URL request 
nocache = Math.random(); 
// Pass the login variables like URL variable 
http.open('get', 'insert.php?site_url='+site_url+'&nocache = '+nocache); 
http.onreadystatechange = insertReply; 
http.send(null); 
} 
function insertReply() { 
if(http.readyState == 4){ 
var response = http.responseText; 
// else if login is ok show a message: "Site added+ site URL". 
document.getElementById('insert_response').innerHTML = 'Site added:'+response; 
} 
} 

我一直在使用沒有Ajax的addinto MySQL和從未有過的URL問題, 「&」的標誌。我想知道什麼可能會導致此問題。

回答

0

我解決它通過簡單地將字組件econdeURI像這樣:

變種SITE_URL = encodeURIComponent方法(的document.getElementById( 'SITE_URL')值);

我只在這裏發佈時,我完全跺腳,所以我很抱歉浪費你的時間!但感謝您花時間檢查我的問題

1
document.getElementById('insert_response').innerHTML = "Just a second..." 

在那裏沒有線路終端?

也可能嘗試像這樣實例化site_url: url site_url = @「thesite.com」;

+0

謝謝,我添加了行終止。 – user1294097 2012-07-16 03:49:51