2
votes

I am using Codeiginter with Summernote on front end - Now In a form, when some fields are empty, the same page loads with validation checking. I am using the following JS and CI code:

 $(window).load(function(){
        <?php if(isset($article['text']))
         {
         ?>
        $('#summernote').code('<?php echo $article['text'];  ?>');
        <?php } else { ?>

        $('#summernote').code('<?php echo $_POST['contents'];  ?>').text();

        <?php } ?>
    });

Now the issue is when say, validation fails, the above code populates the Summernote editor with RAW HTML instead of formatted HTML as it should

This is an example of my first post Pellentesque libero tortor, tincidunt et, tincidunt eget, semper nec, quam. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Pellentesque commodo eros a enim. Aenean imperdiet. Cras sagittis.
Nam at tortor in tellus interdum sagittis. Nullam nulla eros, ultricies sit amet, nonummy id, imperdiet feugiat, pede. Etiam ut purus mattis mauris sodales aliquam. Curabitur blandit mollis lacus. Phasellus tempus.

Any ideas why is this so? I have tried .val and .html - but they don't work. Kindly help.

4
If html() doesn't work we need the output of <?php echo $article['text']; ?> probably you have something weird theremichelem
remove .text() from your else block and try again to see if it works.Kamran
I tried to remove .text() hasn't worked. @Michelem: $_POST['contents'] is applied when validation fails.kirobo

4 Answers

2
votes

With all respect to your own answer I have a few more things to say too since this is a generic problem. Frankly it is not so stupid, modern frameworks does this, they escape the html chars if you don't tell to do otherwise.

In your case(Codeigniter) telling not to escape is in validation part.

In my case(.net mvc razor views) you can do:

$('#summernote').code("@Html.Raw("<div>your html string</div>")");
1
votes

Okay. After two days of intense efforts - I found out the reason - it's stupid. Codeigniter form-validation encodes all formatted data with escape characters and converts them into raw HTML. To solve this problem, remove the validation for contents in your form_validation.php or your form validation rules. Then you can output using

 $('#summernote').code(<?php echo $this->input->post('contents');?>);

Thanks everybody for help, though.

0
votes

Change code with html:

$(window).load(function(){
    <?php if(isset($article['text']))
     {
     ?>
    $('#summernote').html('<?php echo $article['text'];  ?>');
    <?php } else { ?>

    $('#summernote').html('<?php echo $_POST['contents'];  ?>');

    <?php } ?>
});
0
votes

you can use below code

$(window).load(function(){

    // set content 
    <?php if(isset($article['text'])) { ?>
    $('#summernote').html('<?php echo $article['text'];  ?>');
    <?php } else { ?>
    $('#summernote').html('<?php echo $_POST['contents'];  ?>');
    <?php } ?>

    // create editor 
    $("#summernote").summernote();
});

or

 $(window).load(function(){
    // create editor 
    $('#summernote').summernote();

    // set content 
    <?php if(isset($article['text'])) { ?>
    $('#summernote').code('<?php echo $article['text'];  ?>');
    <?php } else { ?>
    $('#summernote').code('<?php echo $_POST['contents'];  ?>');
    <?php } ?>
});