如何将更新的CKEditor内容保存到数据库?

发布于 2024-11-30 16:51:32 字数 1010 浏览 1 评论 0原文

我在谷歌和这个网站上到处搜索这个问题的答案,但没有运气。

我正在为想要自己维护的未来客户开发定制 CMS。下面是将当前页面内容从数据库加载到文本区域的代码:

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 } 
    }
} ?>

这是 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);
}
?>

问题是 POST['content'] 不断获取原始内容,而不是原始内容编辑了用户刚刚提交的一份。

我该如何解决这个问题?

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:

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:

<?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.

How do I fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

长不大的小祸害 2024-12-07 16:51:32

发布表单时,数据应自动传递。以下是开发人员指南中的“集成”页面,对此进行了解释:
http://docs.cksource.com/CKEditor_3.x/Developers_Guide/Integration

您确定查询正在成功运行并且内容字段正在更新吗?我问是因为您发布的代码说在 save.php 中您正在使用:

$id = $_GET['id'];

这似乎会导致问题,因为您在表单中使用的是 method="post",而不是 method="get"。

您还可以发布您用来用 CKEditor 替换 textarea 的代码吗?您使用的是 PHP 方法:

<?php
$CKEditor = new CKEditor();
$CKEditor->basePath = '/ckeditor/';
$CKEditor->replace("content");
?>

还是 JavaScript 方法:

<script language="Javascript">
<!--
  CKEDITOR.replace( 'content' );
//-->
</script>

好吧,
Joe


跟进评论中的问题

嗨 Purmou,

我没有注意到您在表单操作中包含了 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>

在 CKEditor 安装的 _samples 文件夹中,有一些关于通过 PHP 加载编辑器的好文档:
http://YourSite.com/ckeditor/_samples/php/

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");
?>

与 JavaScript 方法类似,您可以在替换文本区域之前添加配置选项。 “advanced.php”文件中的示例:

$CKEditor->config['width'] = 600;

要将 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");

    }
} ?>

我将 textarea id 从“editor”更改为“content”。我建议不要使用“editor”作为 id 或名称,因为它在 CKEditor 代码中用于引用 CKEditor 实例。

您可以在加载编辑器的页面、config.js 文件或您自己的自定义配置文件中进行配置设置。

我花了一些时间尝试在提交表单后捕获表单内容字段的值,但只能在 CKEditor 更新内容之前才能看到它。

祝你好运,

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

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:

$id = $_GET['id'];

This would seem to cause a problem, because you are using method="post" in your form, not method="get".

Could you also post the code you are using to replace the textarea with CKEditor. Are you using the PHP method:

<?php
$CKEditor = new CKEditor();
$CKEditor->basePath = '/ckeditor/';
$CKEditor->replace("content");
?>

Or the JavaScript method:

<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:

<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/

http://YourSite.com/ckeditor/_samples/php/replace.php, has the basic settings:

<?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:

$CKEditor->config['width'] = 600;

To use the PHP method with your specific code, do this:

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.

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.

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.

Be Well,
Joe

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文