Everything good
Everything good

Reputation: 321

Get value of textarea using ckeditor, Ajax

I have this form for a blog. It works well with ajax no refresh, just serialize and submit data. Then i decided using ckeditor 5 the value of the textarea isnt submitted.

my ajax script

$(document).ready(function(){
    $("#create").click(function(event) {
        event.preventDefault();

        var form = $("#mytest");
        var formData = new FormData($("#mytest")[0]);
        $.ajax({
          url  : "test3.php",
          type : "POST",
          cache: false,
          contentType : false,
          processData: false,
          data: formData,
          success:function(response){
            $(".test").html(response);
          }
        });
    });
});

my html form

<form id="mytest" action="">
    <input type="number" name="number" value="">
    <br>
    <textarea class="editor" name="textarea"></textarea>
    <input type="submit" id="create" value="submit content">
</form>

<p class="test"></p>

my php code

$textarea = $_POST["textarea"];
$number = $_POST["number"];

echo $textarea;
echo $number;

Upvotes: 0

Views: 1127

Answers (2)

Everything good
Everything good

Reputation: 321

Give the textarea an id of mycontent.

        $(document).ready(function(){
            $("#create").click(function(event) {
                event.preventDefault();

                $('#myContent').val($('.ck-content p').text());

                var form = $("#mytest");
                var formData = new FormData($("#mytest")[0]);
                $.ajax({
                  url  : "test3.php",
                  type : "POST",
                  cache: false,
                  contentType : false,
                  processData: false,
                  data: formData,
                  success:function(response){
                    $(".test").html(response);
                  }
                });
            });
        });

Thats the way i did it...

Upvotes: 0

user2463644
user2463644

Reputation: 221

CKEditor wraps textarea in its own html markup, so in order to get that textarea text try:

$('.ck-content p').text();

In one of the earlier CKEditor versions I do

$('iframe').content().find('body').text();

Upvotes: 1

Related Questions