Save data to PHP / Mysql with inline edit in CKEditor

≯℡__Kan透↙ 提交于 2019-11-30 10:10:17

You need some AJAX magic. Via JavaScript inside the page you get the edited HTML. Then you send it to the server where a PHP script gets it and can pass it onto MySQL.

Here is a simple test case which will show you the ropes.

Let's start with the editable HTML.

<div id='textToBeSaved' contenteditable='true'>
    <p>Using the <strong>Terminal</strong> in OS X makes you all-powerful.</p>
</div>

We also need a "Save" button which will be used to start the POST event.

<button onclick='ClickToSave()'>Save</button>

Such a button could well we positioned in the CKEditor toolbar itself, but that would require more coding and I'll leave it to someone who's better at JavaScript than me.

Of course you want to include CKEditor. For my sample code I'll also make use of jQuery, which I'll use for AJAXing the results.

<script src='//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js' type='text/javascript'></script>
<script type='text/javascript' src='CKEditor4/ckeditor.js'></script>

Now the script which will execute when you press the "Save" button. It will use CKeditor to grab the edited HTML, then jQuery to send it.

<script type='text/javascript' language='javascript'>
// <![CDATA[
function ClickToSave () {
    var data = CKEDITOR.instances.textToBeSaved.getData();
    $.post('save.php', {
        content : data
        })
    }
// ]]>

This is it, you don't need anything else clientside.

On the server, you must have the PHP code which will act when the script POSTs the updated HTML. The script must be called save.php and be positioned in the same directory where the HTML is if you use my code verbatim. My one-liner here will just save your HTML in a temporary file inside the /tmp folder. Feel free to add your MySQL magic instead.

<?php
file_put_contents('/tmp/serverside.html', $_POST['content']);
?>

This is the way I've done it in the past:

The current_page_id relates to the table row we wish to update, otherwise we wouldn't know what record we need to update.

<div id="editable">My body text</div>
<input type="hidden" name="page_id" id="current_page_id" value="10" />

<script type="text/javascript">

CKEDITOR.disableAutoInline = true;
CKEDITOR.inline('editable', {
    on: {
        blur: function( event ) {

            var params = {
                page_id: $("#current_page_id").val(),
                body_text: event.editor.getData()
            };

            $.ajax({
                url: 'php_file_to_post_to.php',
                global: false,
                type: "POST",
                dataType: "text json",
                data: params,
                success: function(result) {
                    console.log(result);
                }
            });

        }
    }
});

</script>

The inside of your php_file_to_post_to.php PHP file you simply catch the ajax post request and update the row based off of the page_id variable which has also been posted along with the edited content.

This is how you will get text area data

CKEDITOR.instances.editor1.getData()

CKEDITOR is nothing but the object you used to create functionality.

Thanks so much for the code. Try to improve de code with file_put_contents('page.php', stripslashes($_POST['content'])); And add to the div onBlur="ClickToSave()" and now you can to delete de save button.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!