如何使用Ckeditor和CodeIgniter保存文本区域的内容?

时间:2019-03-25 01:10:03

标签: ckeditor codeigniter-3 ckeditor4.x

我在Ckeditor中使用Codeigniter。我的问题是,提交内容时,来自textarea的数据未存储在数据库中。但是,当我再次尝试时,它终于做到了。所以情况就像我必须双击“提交”按钮以保存它。

我将下载的Ckeditor存储在名为./Assests/Ckeditor的文件夹中(很抱歉,拼写错误。稍后将对此进行修复。)

这是我的视图文件夹中的表单:

ask_view.php:

    <form id="form" enctype="multipart/data" method="post"  onsubmit="createTextSnippet();">


                    <div class="form-group"> 
                        <label for="exampleInputEmail1">Title</label>
                        <input type="text" name ="title" class="form-control" id="title"  placeholder="Title" required >
                    </div>


                    <input type="hidden" name="hidden_snippet" id="hidden_snippet" value="" />

                    <div class="form-group"> 
                        <label for="exampleInputEmail1">Editor</label>
                        <textarea name ="text" class="form-control" id="text" rows="3" placeholder="Textarea" required></textarea>
                    </div>
                     <input type="submit" class="btn "  name="submit" value ="Submit" style="width: 100%;background: #f4a950;color:#161b21;">


                </form>

<script src="<?php echo base_url('assests/js/editor.js')?>"></script>

<script type="text/javascript">
    CKEDITOR.replace('text' ,{
        filebrowserBrowseUrl : '<?php echo base_url('assests/filemanager/dialog.php?type=2&editor=ckeditor&fldr=')?>',
        filebrowserUploadUrl : '<?php echo base_url('assests/filemanager/dialog.php?type=2&editor=ckeditor&fldr=')?>',
        filebrowserImageBrowseUrl : '<?php echo base_url('assests/filemanager/dialog.php?type=1&editor=ckeditor&fldr=')?>'
                                    }
                                    );
</script>

<script type="text/javascript">

//code used to save content in textarea as plain text

function createTextSnippet() {

    var html=CKEDITOR.instances.text.getSnapshot();
    var dom=document.createElement("DIV");
    dom.innerHTML=html;
    var plain_text=(dom.textContent || dom.innerText);

    var snippet=plain_text.substr(0,500);
    document.getElementById("hidden_snippet").value=snippet;

    //return true, ok to submit the form
    return true;
}
</script>


  <script type="text/javascript">

    $('#form').on('submit', function (e) {
        e.preventDefault();

        $.ajax({
            type: 'POST',
            url: '/knowmore2/index.php/ask_controller/book_add',
            data: $('form').serialize(),
            success: function (data) {  
               console.log(JSON.parse(data));

            }
        });
    });
</script>


Ask_model.php:

 public function book_add($data)
    {
        $query=$this->db->insert('article', $data);
        return $query;

}

Ask_controller.php:

public function book_add(){
        $data = $_POST;

        $details = array();

        $details['title'] = $data['title'];
        $details['content'] = $data['text'];
        $details['snippet'] = $data['hidden_snippet'];
    $details['createdDate']=date('Y-m-d H:i:s');

        $result=$this->ask_model->book_add($details);

        echo json_encode($details);
    }

带有html标记的内容应保存在数据库中名为content的列中,但第一次单击时未保存。它仅保存在第二个数据上,而其他数据则保存在第一个数据中(如标题等)。因此,我得到了两行数据,其中一列没有内容,另一列有内容。

数据库:

database_image

0 个答案:

没有答案