Why can’t I pass summernote name="" via ajax

Asked

Viewed 536 times

0

After some tests I saw that summernote does not pass the name="" via ajax request, someone can tell me why?

$(document).ready(function(){
    $(document).on('click', '#submit_btn', function(){
        var titulo = $('#titulo').val();
        var dica = $('#dica').val();
        $.ajax({
            url: 'salvar_dica.php',
            type: 'POST',
            data: {
                'save': 1,
                'titulo': titulo,
                'dica': dica,
            },
            success: function(response){
                $('#titulo').val('');
                $('#dica').val('');
                $('#display_area').append(response);                
            }
        });
    });
});

<textarea type="text" name="dica" id="dica" class="summernote"  class="form-control"></textarea>

$(document).ready(function() {
    $('.summernote').summernote();
});

I ran without ajax and everything went well, so I realized I can not pass HTML tag via ajax!

3 answers

2

When typing the text via summernote it will not be inserted in the <textarea>, but in the generated editor, so when trying to take the textarea with .val it will return empty.

To get from summernote use their API as indicated in the documentation: https://summernote.org/getting-started/#basic-api

Example:

var dica = $('<seu elemento>').summernote('code'); //Pega o conteudo HTML gerado

Testing:

$(document).ready(function() {
    var editor = $("#editor");
    editor.summernote();
    
    $("#getText").click(function () {
        console.log( editor.summernote('code') );
    });
});
<!-- include libraries(jQuery, bootstrap) -->
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script> 
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.js"></script> 

<!-- include summernote css/js -->
<link href="https://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.9/summernote.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.9/summernote.js"></script>


<textarea id="editor"></textarea>

<button id="getText">Ver texto</button>

  • Guilherme, I did the way you told me, but in my console he returns me an Uncaught Rangeerror: Maximum call stack size exceeded

  • @This should be another problem, there’s some online link to it?

  • Guilherme, I don’t have no, I’m running via wamp.

  • @Marcospereira sends the entire HTML and Ajax code to Pastebin.com and then puts the link here in the comments for me to see

  • Ta ai Guilherme: https://pastebin.com/YGVXZGMa

1

Ajax normally sends the contents of textarea used by Summernote, and backend, for example PHP, the content of textarea is received by your name, as in any form element, with $_POST['dica'];.

I see in this part $('#dica').val(''); of sponse Ajax you want to clear the text in the editor, but this will only clear the contents of the textarea.

To clear the text of the Summernote editor (latest versions) you can use:

$("#dica").summernote("reset");

Soon your code would look like this:

$(document).ready(function(){
    $(document).on('click', '#submit_btn', function(){
        var titulo = $('#titulo').val();
        var dica = $('#dica').val();
        $.ajax({
            url: 'salvar_dica.php',
            type: 'POST',
            data: {
                'save': 1,
                'titulo': titulo,
                'dica': dica,
            },
            success: function(response){
                $('#titulo').val('');
                $('#dica')
                .val('')  // apaga o texto do textarea usado pelo summernote
                .summernote("reset"); // apaga o texto do editor
                $('#display_area').append(response);                
            }
        });
    });
});

Functional example

  • RESOLVED!!!

  • Hello Mark! Mark the answer that solved the problem so that the question is not open. Obg!

  • Just click on the left side of the answer.

0

textarea does not return in .val(), use:

var dica = $('#dica').text();
  • 1

    https://jsfiddle.net/w27zq98j/

  • @Valdeirpsr the problem is in using with summernote, i explained in the answer https://answall.com/a/291138/3635

Browser other questions tagged

You are not signed in. Login or sign up in order to post.