dtvfxzq3802 2018-03-06 23:05
浏览 87
已采纳

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

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

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

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

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").click(function(){
console.log($("#trumbowyg-demo").html());

});

so what i need is this content saved to my database template table which has 3 columns; 'an id, a foreign key id for the user, and the template style..

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!

</div>
  • 写回答

1条回答 默认 最新

  • doudi4014 2018-03-08 16:09
    关注

    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:

    $("#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;
        }
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作