2013-11-01 32 views
0

我已經搜索了Stackoverflow,發現了一些類似的問題,但無法理解答案。我只在2天前學過PHP,所以請放心。

我的問題是,我正在使用一個窗體供用戶編寫並提交一些文本到文件中。當他們點擊提交時,文本被寫入文件,下次他們訪問表單時,文本將保留在表單中供他們修改。問題是,如果任何形式的報價被輸入並提交,那麼會在其前面添加一個\。

例子: 用戶寫道:「你好」 用戶點擊提交 形式重新加載和「你好」輸出爲\「你好\」

我知道\取消了的東西,如報價單,以便它可以以字符串的形式輸出,但是可以使用$ _POST來存儲表單數據,並且每當我使用引號時都不會有\。

請讓你的解釋詳細,因爲我只是PHP的新手。提前致謝。下面是代碼造成我的問題:

echo '<div class="wrap">'; 

// makes an if statement to check if the submit button has been pushed 
if (isset($_POST['Submit1'])) { 

// opens the include file and writes the contents of "left-form" to the file. left-form is the name of the html text area 

$file = fopen($_SERVER['DOCUMENT_ROOT'] . '/wp-content/themes/mytheme/options/index_left.php', "w"); 
fwrite($file,$_POST["left_form"]); 
fclose($file); 

}

echo '<p><form method="POST" action=""><textarea rows="4" cols="50" name="left_form">'; 
// includes the contents of the file being written to as the text inside the text field 
include $_SERVER['DOCUMENT_ROOT'] . '/wp-content/themes/mytheme/options/index_left.php'; 
echo '</textarea></p>'; 
echo '<input type="submit" value="Submit" name="Submit1"></form>'; 


// sets an if statement to check if the post button has been hit to display a message that it has been posted 
if (isset($_POST['Submit1'])) { 
echo htmlspecialchars($_POST["left_form"]) . '</br>HAS BEEN POSTED'; 
} 

echo '</div>'; 

}

+0

'var_dump(get_magic_quotes_gpc());'output? –

+0

我在代碼的頂部寫了這個,它沒有區別,但是我在文本字段上面寫了int(0)。 – user2946010

+0

然後它沒有啓用。 –

回答

1

htmlspecial字符將追加\逃避一些符號包裝在一個stripslashes()函數。

echo stripslashes(htmlspecialchars($_POST["left_form"])) . '</br>HAS BEEN POSTED'; 

編輯:

響應您的評論,如果文本輸入被存儲到數據庫如MySQL的,請嘗試使用周圍的mysql_real_escape_string()函數和stripslash()。否則,你沒有htmlspecialchars()函數試過嗎?

+0

哦,是的,這似乎已經工作(抱歉前面的評論說,這不是我在執行代碼時犯了錯誤)。感謝您的幫助:D。我怎樣才能使它工作,以便斜線從寫入文件的文本中除去? – user2946010

+0

你是什麼意思寫入文件?像寫入數據庫? –

+0

如果你看看我的第二條代碼註釋下的代碼,它會開始:「//打開包含文件」,你將看到文本框的內容被寫入一個名爲「index_left.php」的文件中。 index_left.php也用於填充文本區域。在我剛剛提到的評論下的3行代碼將其寫入文件中。它將$ _POST內存儲的內容發佈到文本文件中。謝謝。 – user2946010