开发者

How to save updated CKEditor content to database?

开发者 https://www.devze.com 2023-03-29 05:45 出处:网络
I\'ve searched high and low both on Google and this site for an answer to this question with no luck.

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 开发者_StackOverflow中文版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?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消