Can't get value from Summernote textarea using Javascript AJAX send post data
Asked Answered
G

2

6

I've been checking summernote API, and there is my code

<form id="mail">
  <input type="text" id="email">
  <textarea id="summernote"></textarea>
  <input type="submit">
</form>
$('#summernote').summernote({
  placeholder: 'your Message',
  tabsize: 2,
  height: 300
});

$('document').ready(function() {
  var messageData = $('#summernote').summernote('code');
  var email = $('#email').val();
  $(function() {
    $('#mail').submit(function(event) {
      event.preventDefault();
      $.ajax({
        url: 'sendmail.php',
        type: 'post',
        data: {
          'send_mail': 1,
          'to': email,
          'message': messageData,
        },
        success: function(response) {
          if (response == 'success') {
            //my success response
          } else if (response == 'error') {
            //my error response
          }
        }
      });
    });
  });
});

My problem is the message was successfully sent, but I didn't get that text area value, just an empty email. Please help.

Note: that I'm using phpMailer for sending email.

Garrido answered 23/1, 2018 at 10:1 Comment(3)
some things to check for is your summernote initialization is out of $(document).ready()..; you are nesting $(function() {.. in $(document).ready.. which is not required. So remove that. And add your summernote initializer code inside $(document).ready..Spectrochemistry
ok I'll try. thank you for your answer.Garrido
oh thank you it worked! thank you very much sir!Garrido
E
7

Change the code as below:

  1. Initialize the "summernote" inside the ready function.
  2. Fetch the summer note value inside the mail submit event

Code as follows:

$('document').ready(function(){

    $('#summernote').summernote({
       placeholder: 'your Message',
       tabsize: 2,
       height: 300
    });


     $(function(){
         $('#mail').submit(function(event){

           var messageData = $('#summernote').summernote('code');
           var email = $('#email').val();

          event.preventDefault();
          $.ajax({ 
           url: 'sendmail.php',
           type: 'post',
           data: { 
            'send_mail' : 1, 
            'to' : email,
            'message' : messageData, 
           }, 
           success: function(response){ 
            if(response == 'success'){ 
            //my success response
             } else if (response == 'error'){
            //my error response
             }
            }
           });
          });
     });
});
Eyed answered 23/1, 2018 at 10:13 Comment(0)
S
-1

var paragraph = document.getElementsByClassName("note-editable card-block")[0].setAttribute("id","paragraph");
$.ajax({
            type: 'POST',
            url: 'post.php',
            data:  {"summernote": $('#summernote').html()},
            
            
            success: function(response) {

                $("#result").text(response);
                $("#result").attr("class","text-success");
            },
            error: function( jqXhr, textStatus, errorThrown ){
                console.log( errorThrown );
                $("#result").text("good");
                $("#result").attr("class","text-success");
            }

        });
Selfhypnosis answered 6/8, 2020 at 10:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.