简体   繁体   中英

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

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!

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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