简体   繁体   English

CodeIgniter - 将 WYSIWYG 编辑器的 HTML 内容保存在数据库中以供日后使用

[英]CodeIgniter - Save HTML content of WYSIWYG editor in a database to be used at a later date

This may be a longshot but I am struggling to find help online and I am genuinely lost ..这可能是一个长期目标,但我正在努力在网上寻求帮助,而且我真的很迷茫..

I am building a CodeIgniter Web App where users can sign in, and create email templates, then send them to contacts..我正在构建一个 CodeIgniter Web 应用程序,用户可以在其中登录并创建电子邮件模板,然后将它们发送给联系人。

I am using the Trumbowyg Editor for creating the email templates which I have found quite good and flexible, i have pre-made templates the users can select and edit if they please..我正在使用Trumbowyg 编辑器创建电子邮件模板,我发现它非常好且灵活,我有预制的模板,用户可以根据需要选择和编辑..

what i want however is for a user to create their own template, or make edits to an existing one, and save it to be able to come back at a later date.. I think it is possible to save it to my database, I have a template table setup correctly with foreign keys etc and i have the 'templatestyle' field type set as 'blob', in order to save the content here..然而,我想要的是让用户创建自己的模板,或对现有模板进行编辑,然后将其保存以便日后返回。我认为可以将其保存到我的数据库中,我使用外键等正确设置模板表,并且我将“模板样式”字段类型设置为“blob”,以便在此处保存内容..

I am able to get the contents of the wysiwyg as when I test out this code of clicking the saveContent button I get the current content in the console.log;我能够获得所见即所得的内容,因为当我测试单击 saveContent 按钮的代码时,我在 console.log 中获得了当前内容;

 $("#saveContent").click(function(){ console.log($("#trumbowyg-demo").html()); });

so what i need is this content saved to my database template table which has 3 columns;所以我需要的是将此内容保存到我的数据库模板表中,该表有 3 列; 'an id, a foreign key id for the user, and the template style.. '一个 id、用户的外键 id 和模板样式..

I realise there is a lot in here and any code provided in order to help me set this up to save my database will be massively appreciated我意识到这里有很多东西,提供任何代码以帮助我设置它以保存我的数据库将不胜感激

thanks in advance!提前致谢!

Generally speaking you just have to send a simple post with one var to a controller method that receives the post var and inputs it in the database.一般来说,您只需将一个带有一个变量的简单帖子发送到一个控制器方法,该方法接收帖子变量并将其输入到数据库中。

JS: JS:

$("#saveContent").click(function () {
    var content = $("#trumbowyg-demo").html();
    $.ajax({
        type: 'POST',
        url: '/somecontroller/add',
        data: {
            content: content
        },
        dataType: 'json',
        success: function (data) {
            if (data.status == 'error') {
                alert('An error occured: ' + data.msg);
            } else {
                alert('Success: ' + data.msg)
            }
        }
    });
});

PHP: PHP:

class Somecontroller extends CI_Controller {

    public function add() {

        $content = $this->input->post('content');

        if (empty($content)) {
            echo json_encode(array('status' => 'error', 'msg' => 'Content field cannot be empty!'));
            exit;
        }

        // db functions should be move to a model
        // probably would be a good idea to filter $content somehow
        // all the db insert does is escape
        $this->db->insert('sometable', array('somefield' => $content));

        echo json_encode(array('status' => 'success', 'msg' => 'Item added with id: ' . $this->db->insert_id()));
        exit;
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM