php 如何将更新的 CKEditor 内容保存到数据库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7131695/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to save updated CKEditor content to database?
提问by Purag
I've searched high and low both on Google and this site for an answer to this question with no luck.
我在谷歌和这个网站上搜索了高低,寻找这个问题的答案,但没有运气。
I'm working on a custom CMS for future customers who want to do their own maintenance. Here's the code that loads the current page content into the textarea from the database:
我正在为想要自己维护的未来客户开发自定义 CMS。这是将当前页面内容从数据库加载到 textarea 的代码:
if(isset($_GET['id'])) {
$id = $_GET['id'];
$content = mysql_query("SELECT title, content FROM pages WHERE id=".$id);
$search = mysql_num_rows($content);
if($search > 0){
while($page = mysql_fetch_array($content)) { ?>
<h1>Editing <?php echo $page['title']; ?></h1>
<form id="editform" action="save.php?id=<?php echo $_GET['id']; ?>" method="post">
<textarea id="editor" name="content"><?php echo $page['content']; ?></textarea>
</form>
<?php }
}
} ?>
Here's the code in save.php
:
这是中的代码save.php
:
<?php
if(isset($_POST['content'])){
$content = $_POST['content'];
$id = $_GET['id'];
echo $content;
mysql_query("UPDATE pages SET content=".$content." WHERE id=".$id);
}
?>
The problem is that the POST['content'] keeps getting the original content, not the edited one that the user just submitted.
问题是 POST['content'] 不断获取原始内容,而不是用户刚刚提交的编辑内容。
How do I fix this?
我该如何解决?
回答by codewaggle
The data should be passed automatically when the form is posted. Here is the "Integration" page page from the Developers Guide that explains this:
http://docs.cksource.com/CKEditor_3.x/Developers_Guide/Integration
提交表单时应自动传递数据。这是开发人员指南中的“集成”页面,解释了这一点:http:
//docs.cksource.com/CKEditor_3.x/Developers_Guide/Integration
Are you sure that the query is being run successfully and that the content field is being updated? I ask because the code you posted says that in save.php, you are using:
您确定查询正在成功运行并且内容字段正在更新吗?我问是因为您发布的代码说在 save.php 中,您正在使用:
$id = $_GET['id'];
This would seem to cause a problem, because you are using method="post" in your form, not method="get".
这似乎会导致问题,因为您在表单中使用的是 method="post",而不是 method="get"。
Could you also post the code you are using to replace the textarea with CKEditor. Are you using the PHP method:
您还可以发布您用来用 CKEditor 替换 textarea 的代码吗?你在使用PHP方法吗:
<?php
$CKEditor = new CKEditor();
$CKEditor->basePath = '/ckeditor/';
$CKEditor->replace("content");
?>
Or the JavaScript method:
或者 JavaScript 方法:
<script language="Javascript">
<!--
CKEDITOR.replace( 'content' );
//-->
</script>
Be Well, Joe
加油,乔
Follow up to question in comments
在评论中跟进问题
Hi Purmou,
嗨,珀穆,
I didn't notice that you were including the id in the form action, sorry about that. If you did want to use it as a $_POST variable instead, you could include it as a hidden field like this:
我没有注意到您在表单操作中包含了 id,对此感到抱歉。如果您确实想将其用作 $_POST 变量,则可以将其作为隐藏字段包含,如下所示:
<form id="editform" action="save.php?id=<?php echo $_GET['id']; ?>" method="post">
<input type="hidden" id="id" name="id" value="<?php echo $_GET['id']; ?>" />
<textarea id="editor" name="content"><?php echo $page['content']; ?></textarea>
There is some good documentaion about loading the editor via PHP in the _samples folder of the CKEditor install: http://YourSite.com/ckeditor/_samples/php/
在 CKEditor 安装的 _samples 文件夹中有一些关于通过 PHP 加载编辑器的很好的文档:http: //YourSite.com/ckeditor/_samples/php/
http://YourSite.com/ckeditor/_samples/php/replace.php, has the basic settings:
http://YourSite.com/ckeditor/_samples/php/replace.php,具有基本设置:
<?php
// Include the CKEditor class.
include_once "ckeditor/ckeditor.php";
// Create a class instance.
$CKEditor = new CKEditor();
// Path to the CKEditor directory.
$CKEditor->basePath = '/ckeditor/';
// Replace a textarea element with an id (or name) of "textarea_id".
$CKEditor->replace("textarea_id");
?>
Similar to the JavaScript method, you can add config options before you replace the textarea. An example from the "advanced.php" file:
与 JavaScript 方法类似,您可以在替换 textarea 之前添加配置选项。“advanced.php”文件中的一个例子:
$CKEditor->config['width'] = 600;
To use the PHP method with your specific code, do this:
要在您的特定代码中使用 PHP 方法,请执行以下操作:
if(isset($_GET['id'])) {
$id = $_GET['id'];
$content = mysql_query("SELECT title, content FROM pages WHERE id=".$id);
$search = mysql_num_rows($content);
if($search > 0){
while($page = mysql_fetch_array($content)) { ?>
<h1>Editing <?php echo $page['title']; ?></h1>
<form id="editform" action="save.php?id=<?php echo $_GET['id']; ?>" method="post">
<textarea id="content" name="content"><?php echo $page['content']; ?></textarea>
</form>
<?php }
include_once "ckeditor/ckeditor.php";
$CKEditor = new CKEditor();
$CKEditor->basePath = '/ckeditor/';
$CKEditor->replace("content");
}
} ?>
I changed the textarea id from "editor" to "content". I would recommend against using "editor" for the id or name, because it's used in the CKEditor code to refer to the CKEditor instance.
我将 textarea id 从“编辑器”更改为“内容”。我建议不要对 id 或名称使用“editor”,因为它在 CKEditor 代码中用于引用 CKEditor 实例。
You can do config settings in the page where you load the editor or in the config.js file or in your own custom config file.
您可以在加载编辑器的页面或 config.js 文件或您自己的自定义配置文件中进行配置设置。
I spent some time trying to catch the value of the form content field after the form is submitted, but was only able to see it before CKEditor had updated the contents.
我花了一些时间尝试在提交表单后捕获表单内容字段的值,但只能在 CKEditor 更新内容之前看到它。
Be Well,
Joe
加油,
乔