【发布时间】:2014-02-23 12:47:36
【问题描述】:
我有一个页面,可以将文本文件中的 html 代码加载到 textarea 中,我需要能够使用脚本保存它的内容。
我正在使用 PHP 脚本从文件加载代码并将其回显到 textarea,但是如何将内容发送回脚本并将其保存到同一个文件或保存到带有新名字?
我在想 getElementById 是否会帮助我,但我不确定如何。
加载脚本(也可以删除文件)
// The file hierarchy:
//
//Web root - admin - This file
// - pages - in here are the page text files
// The variable pagesList is the filename chosen in a dropdown list earlier
$page = $_GET["pagesList"];
$action = $_GET["action"];
//get the path to the page( all pages are in a folder named 'pages')
$filename = dirname(dirname(__FILE__))."/pages/".$page;
if(file_exists($filename) and is_file($filename)){
//If I want to load a file
if($action == "open"){
$f = fopen($filename,"rt");
$content = fread($f, filesize($filename));
echo $content;
@fclose($f);
//If I want to delete a file
}elseif($action == "delete" && is_file($filename)){
//move the working directory to where the file is
$old = getcwd();
chdir(dirname(dirname(__FILE__))."/pages/");
//----
if(unlink($filename))
echo "File deleted,".$filename;
else
echo "Error deleting file!";
//change back the working directory to where it was
chdir($old);
//If I want to save a file
}elseif($action == "save"){
if(file_exists($filename)){
//Unknown script, need help!
}else{
}
}
}
文本区域只有一行,其中包含一个包含:
<textarea id="html_content" style="width:600;height:200;"><?php include("loader.php") ?></textarea>
总结一下:我需要帮助将文本区域的内容保存到脚本中以便以后保存。
编辑:感谢 davidkonrad,我只需要在脚本中添加一些 POST 接收并添加 file_put_content 以及发送给它的内容。
出现的问题是 jQuery 显然将 \ 放在每个 " 或 ' 之前。这弄乱了所有应该是干净和有效的 html 代码。我必须以 " 以某种方式替换 \",str_replace 不会剪掉它。有什么想法吗?
EDIT2:再次感谢 davidkonrad 通过使用 encodeURIComponent(jQuery) 客户端和 urldecode(PHP) 服务器端修复它。
【问题讨论】:
标签: php html file save textarea