Search code examples
phpcodeigniterinputckeditorajaxform

cannot input textarea with CKeditor on Codeigniter


when i want to input textarea with CKeditor , the string is Null , but when i delete class="CKeditor" in textarea the string successfully filled. i have check in controller and database but it does not matter

My Controller

function AddNews(){
    $data = array(
        'title_news'            => $this->input->post('title_news'),
        'text'                  => $this->input->post('text_news'),
        'date'                  => $this->input->post('date'),
    );
    $insert = $this->m_news->save($data);
    echo json_encode(array("status" => TRUE));       
}

My View

<form action="#" id="form" class="form-horizontal">
    <input type="hidden" value="" name="id_news"/>
        <div class="form-body">
            <div class="form-group">
                <label class="control-label col-md-3">Title News </label>
                <div class="col-md-9">
                    <input name="title_news" placeholder="Title Name" class="form-control" type="text"><span class="help-block"></span>
                </div>
            </div>

            <div class="form-group">
                <label class="control-label col-md-3">Title News </label>
                <div class="col-md-9">
                    <textarea class="ckeditor"  name="text_news" rows="3" placeholder="Enter text . . . "></textarea><span class="help-block"></span>
                </div>
            </div>

            <div class="form-group">
                <label class="control-label col-md-3">Date</label>
                <div class="col-md-9">
                    <div class="input-group">
                        <input class="form-control date-picker" name="date" type="text" data-date-format="yyyy-mm-dd" placeholder="yyyy-mm-dd" /><span class="input-group-addon"><i class="fa fa-calendar bigger-110"></i></span>
                    </div>
                </div>
            </div>
            <button type="button" id="btnSave" onclick="save()" class="btn btn-primary">Save</button>
        </div> 
    </form>

My Ajax Input

function save()
{
    var formData = new FormData($('#form')[0]);
    $.ajax({
        url : "<?php echo base_url('admin-spot/news/AddNews')?>",
        type: "POST",
        data: formData,
        contentType: false,
        processData: false,
        dataType: "JSON",
        redirect: true,
        success: function(data){
            if(data.status){
                $('#modal_form').modal('hide');
            }
            else
            {}

            error: function (jqXHR, textStatus, errorThrown){
                alert('Error adding / update data');
                $('#btnSave').text('save'); //change button text
                $('#btnSave').attr('disabled',false); //set button enable
            }
        });
    }

My Model

function Save($data)
{
    $sql = $this->db->insert($this->table, $data);
    return $sql;
}

hello guys, when I want to input textarea with CKeditor , the string is Null , but when I delete class="CKeditor" in textarea the string successfully filled. I have check in controller and database but it does not matter


Solution

  • Add this to your script

    function save()
    {
        for (instance in CKEDITOR.instances) {
           CKEDITOR.instances[instance].updateElement();
        }
        var formData = new FormData($('#form')[0]);
    
        $.ajax({
            .... 
        });
    }