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

时间:2018-03-06 23:05:55

标签: php jquery codeigniter wysiwyg

这可能是一个远景,但我正在努力寻求在线帮助,我真的迷失了......

我正在构建一个CodeIgniter Web App,用户可以在其中登录并创建电子邮件模板,然后将其发送给联系人..

我正在使用Trumbowyg Editor来创建我发现非常好且灵活的电子邮件模板,我预先制作了模板,用户可以根据需要进行选择和编辑。

我想要的是让用户创建自己的模板,或者对现有模板进行编辑,并将其保存以便以后再回来..我认为可以将它保存到我的数据库,我有一个模板表设置正确与外键等,我有'templatestyle'字段类型设置为'blob',以便在这里保存内容..

我可以获取wysiwyg的内容,因为当我测试单击saveContent按钮的代码时,我获得了console.log中的当前内容;

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

});

所以我需要的是这个内容保存到我的数据库模板表有3列; 'id,用户的外键ID和模板样式..

我意识到这里有很多内容,为了帮助我设置这个以保存我的数据库而提供的任何代码将受到大力赞赏

提前感谢!

1 个答案:

答案 0 :(得分:0)

一般来说,你只需要将一个带有一个var的简单帖子发送到一个控制器方法,该方法接收post var并将其输入数据库。

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:

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

}