Luis
Luis

Reputation: 3277

CKEditor inserts \n automatically

My problem is with CKEditor, it adds \n when I submit data.

To get the data I'm using simply:

CKEDITOR.instances['contentBox'].getData()

Example of data after submit in the DB:

<p>\n   Heloo<br />\n   How are you?</p>\n
<p>\n   Another Subject<br />\n My name is Luis</p>\n

All is fine, just want to cancel the \n. I don't wanna use REGEX or another PHP functions which will remove it.. I prefer solution via the config of CKEditor.

EDIT ::

CKEditor is not the problem (look at David Mulder answer).

Ajax code I created (probably the problem):

$('#edit').live('click', function() {
    if ($("#formValue").valid())
    {
        $('.simplebox').slideUp(200, function() {
            $('body').animate({scrollTop:140}, 350, function() {
                $('#loading-edit').slideDown(300, function() {
                    $.ajax({
                        type: "POST",
                        dataType: "json",
                        url: "../new_lesson_proccess/",
                        data: getDataToPost(),
                        success: function(data){

                            if (data.success == true)
                            {
                                $('#loading-edit').fadeOut(200, function() {
                                    $('.name_news_success').html($('input[name=name]').val());
                                    $('#successfull-edit').fadeIn(200);
                                });
                            }

                        }
                    });
                });
            });
        });
    }
})

function getDataToPost()
{
    var value = CKEDITOR.instances['valuecontent'].getData();

    return {
        id: $('input[name=news_id]').val(),
        tags : $('#tags').textext()[0].tags()._formData,
        name: $('input[name=name]').val(),
        content: value
    }
}

What could be wrong here?

EDIT 2 ::

I'm using CodeIgniter framework.

Part of the controller:

public function new_lesson_proccess()
    {
        // # POST to Array
        $data = array(
            'content' => $this->input->post('content', FALSE)
        );

        $dataExport = array(
            'success' => $this->lessons_model->news_lesson($data)
        );

        echo json_encode($dataExport);
    }

Part of the model lessons_model:

function new_lesson($data)
    {
        $dataInsert = array(
            'content' => mysql_real_escape_string($data['content'])
        );

        if ($this->db->affected_rows($this->db->insert('web_lessons', $dataInsert)) == 1)
            return true;
        else
            return false;
    }

Encoding: UTF-8

The field content in the database is defined as TEXT

I removed some code of validation & etc because it's not important.

Upvotes: 1

Views: 7530

Answers (5)

Andrew Larsen
Andrew Larsen

Reputation: 1267

I know this is an old topic, but I faced the same problem and just wanted to share my solution.

Im using jquery's ajax method, to post the data to my php script, and the content breaklines is changed to \n.

Here is my (workaround) solution, I do this in the php script, before parsing the data to the database:

if (strpos($content,'\n') !== false) {
    $content = str_replace('\n', chr(13), $content);
}

Upvotes: 1

Narendra
Narendra

Reputation: 3117

This might be helpful.

CKEDITOR.on( 'instanceReady', function( ev )
   {
      ev.editor.dataProcessor.writer.setRules( 'p',
         {
            indent : false,
            breakBeforeOpen : true,
            breakAfterOpen : false,
            breakBeforeClose : false,
            breakAfterClose : true
         });
   });

Upvotes: 3

Andre
Andre

Reputation: 2469

Please see this ckeditor output formatters: http://docs.cksource.com/CKEditor_3.x/Developers_Guide/Output_Formatting

Also this answer may help: Removing unwanted newline characters when adding <p> in CKEditor

Upvotes: 1

David Mulder
David Mulder

Reputation: 27030

This might be a bit of a long shot, but I think that this could quite likely be not a setting of ckEditor (as I used ckEditor extensively in the past, and encountered no such problem), but rather a feature (changing character 10 and character 13 to the \n notation) of either the ajax library you're using or the database library. Either way, log and inspect at which point chr(13) is replaced by the \n in the first place and if it's in the ajax library then it only makes sense to parse it back in your php code and if it's in your database script then you would need to change it somewhere inside of there (I wouldn't be too suprised, as I know of people who prefer having no 'hidden' characters in their database like line breaks). Of course this answer only stands if you're trying to get the line breaks to be in the valid format, if you simply don't care about the linebreaks Lee Taylor answer would be the place to go.

Upvotes: 2

Lee Taylor
Lee Taylor

Reputation: 7994

Looks like this might be your solution:

http://dev.ckeditor.com/ticket/3260

Upvotes: 2

Related Questions