2014-02-10 120 views
0

我有這樣定義的格式:

<table> 
<form id="myForm" method="post"> 
<tr><td> Item Name: </td><td><input type="text" name="itemname" id="itemname"/></td></tr> 
<tr><td> Description:</td><td> <input type="text" name="description" id="description"/></td></tr> 
<tr><td> Sell Time:</td><td> <input type="test" name="selltime" id="selltime"/></td></tr> 
<tr><td> Price:</td><td> <input type="test" name="price" id="price"/></td></tr> 
<tr><td> Image URL:</td><td> <input type="test" name="url" id="url"/></td></tr> 
<tr><td><input type="submit" value="Post" name="info" onclick="return post();"></td></tr> 
</form> 
</table> 

正如你所看到的,的onclick它調用的函數post()。其依次讀取文件post.php並將其發送所需參數(itemname,description,selltime, price,url)。最後他們被插入到數據庫中的某個表中。

這裏的文件post.php的開頭:

<?php 
session_start(); 
mysql_connect("localhost","root","password") or die("couldnt connect"); 
mysql_select_db("mynewdb") or die("couladnt find"); 
$output = ''; 
if(isset($_REQUEST['itemname']) && isset($_REQUEST['description']) 
&& isset($_REQUEST['selltime']) && isset($_REQUEST['price']) && 
isset($_REQUEST['url'])){ 
$url = $_REQUEST['url']; 
... 
?> 

問題是url有時有錯。例如,如果我進入這個網址:coca cola

http://www.comax.co.il/Max2000Upload/1443/Prt_Pic%5C1%5C864.jpg

我得到這個插入代替:

http://www.comax.co.il/Max2000Upload/1443/Prt_Pic1864.jpg

這是爲什麼?

編輯:

按照要求,下面是post()功能:

<script> 
function post() 
{ 
    var itemname = document.getElementById('itemname').value; 
    var description = document.getElementById('description').value; 
    var selltime = document.getElementById('selltime').value; 
    var price = document.getElementById('price').value; 
    var url = document.getElementById('url').value; 
    if (window.XMLHttpRequest) 
    { 
    xmlhttp=new XMLHttpRequest(); 
     } 
    else 
    { 
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    xmlhttp.onreadystatechange=function() 
    { 
    if (xmlhttp.readyState==4 && xmlhttp.status==200) 
     document.getElementById("posted").innerHTML=xmlhttp.responseText; 
    } 
    xmlhttp.open("POST","post.php?itemname="+itemname+ "&description="+description+ "&selltime="+selltime 
      +"&price="+price+"&url="+url,true); 
    xmlhttp.send(); 
    return false; 
} 
</script> 
+0

您遺漏了最重要的部分:'post()'javascript函數和'$ url'。 –

+0

我添加了功能代碼。 '$ url'被插入到數據庫中,如果我檢查數據庫,我可以看到'$ url'插入錯誤。 –

回答

0

的 「%5C」 代表 「\」,它可能會逃脫下面的數字,嘗試將它們替換爲「\\」,這應該在URL中保留反斜槓。

+0

有沒有一個'功能'那麼做? –

+0

使用php「str_replace」:http://www.php.net/manual/en/function.str-replace.php。這是一個例子:str_replace('\\','\\\\',$ url)。 – angelobochini