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 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分