Search code examples
javascriptphpjquerycodeigniterckeditor

Add another ckeditor in codeigniter using add button on the page


I want to add another ckeditor whenever you click the add button another text field with ckeditor will be added. When click it added new field already but i cannot type on the new field with ckeditor. Here's my code:

* View *

                   <div id="addanother">
                        <?php echo $this->ckeditor->editor("textheading_lead[]",""); ?>
                        <br>
                        <div class="form-group">
                            <label for="sel1">小見出し</label>
                            <select class="form-control" name="subheading[]">
                                <option value="">監修 one</option>
                                <option value="">監修 two</option>
                                <option value="">監修 three</option>
                                <option value="">監修 four</option>
                            </select>
                        </div>
                        <input type="text" class="form-control" name="subheading_text[]">
                    </div>
                    <div id="addnewdiv"></div>
                    <a href="" class="btn btn-info addfields" >+</a>

* javascript *

$('.addfields').on('click', addfields);
function addfields(e) {
  var new_input = $('#addanother').html();
  var aaa = new_input + '<br>';
  $('#addnewdiv').append(new_input);
  e.preventDefault();
}

* Controller *

public function add_special(){
        $this->load->library('ckeditor');
        $this->load->library('ckfinder');
        $this->ckeditor->basePath = base_url().'asset/ckeditor/';

        $this->ckeditor->config['language'] = 'en';
        $this->ckeditor->config['width'] = '730px';
        $this->ckeditor->config['height'] = '300px';

        $this->ckfinder->SetupCKEditor($this->ckeditor,'../../asset/ckfinder/'); 

        $this->load->view('common/header');
        $this->load->view('admin/special/add_special');
        $this->load->view('common/footer');
    }

link of CKEditor package

https://ckeditor.com/ckeditor-4/download/


Solution

  • Unfortunately ckeditor plugin used to create dynamic id for editor panel. While appending div, plugin finds duplicate editor instances with same id textheading_lead[] and it was throwing error in your case. Here i made some alterations and will work for you.

    <?php
    defined('BASEPATH') OR exit('No direct script access allowed');
    ?><html lang="en">  
    <head>
      <title>How to Integrate CKeditor in Codeigniter using Bootstrap</title>
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
      <script type="text/javascript" src="<?php echo base_url(); ?>assets/ckeditor/ckeditor.js"></script>
      <script type="text/javascript" src="<?php echo base_url(); ?>assets/ckfinder/ckfinder.js"></script>
    </head>
    <body>
    <div class="container">
    
    <form method="post" action="<?php echo base_url(); ?>/Test57619322/here">
        <div class="row">
            <div class="col-lg-12">
                <div id="addanother">
                    <br>
                    <?php echo $this->ckeditor->editor("textheading_lead[0]",""); ?>
                    <br>
                    <div class="form-group">
                        <label for="sel1">小見出し</label>
                        <select class="form-control" name="subheading[]">
                            <option value="">監修 one</option>
                            <option value="">監修 two</option>
                            <option value="">監修 three</option>
                            <option value="">監修 four</option>
                        </select>
                    </div>
                    <input type="text" class="form-control" name="subheading_text[]">                
                </div>
                <div id="addnewdiv"></div>            
            </div>        
        </div>
        <div class="row" style="margin-top:20px;">
            <div class="col-lg-12">
                <a href="" class="btn btn-info addfields" >+ Add Fields</a>
                <input type="submit" name="submitBtn" value="Submit" class="btn btn-success">
            </div>
    
        </div>      
    </form>
    
    </div>
    <script type="text/javascript">
        $('.addfields').on('click', addfields);
        var i=0;
        function addfields(e) {
          e.preventDefault();
          var copy = $('#addanother').clone();
    
          var oneplus=i+1;
    
          $(copy).find('div#cke_textheading_lead\\[0\\]').remove();
          $(copy).find('script').remove();
          $(copy).find('textarea[name=textheading_lead\\[0\\]]').attr('name', 'textheading_lead['+oneplus+']');
    
          $('#addnewdiv').append($(copy).html()+ '<br>');
          CKEDITOR.replace('textheading_lead['+oneplus+']');
          i++;  
    
        }
    </script>
    </body>
    </html>
    

    You can receive response in your controller like $_POST["textheading_lead"] or Codeigniter post method and as usual it gets as an array.