Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

after ajax form submit value from ckeditor textarea is not sent through post

i have a form having some textfields and a textarea (ckeditor), after onclick button art_title field value is sent to art_save.php page, but value from textarea is not sent.

<script src="ckeditor/ckeditor.js"></script>
function saveArt() 
{
    var title = document.getElementById('art_title'),
        art_title = title.value;

    var desc = document.getElementById('art_body'),
        art_body = desc.value;

    jQuery.ajax({
        type: 'POST',
        url: 'art_save.php',
        data: {
            title: art_title,
            aut: art_author,
            tag: art_tag,
            desc: art_body

              }

         });  
         return false; 

 }

html part

<form method="post" name="art" id="art">
    <input type="text" id="art_title" name="art_title" placeholder="Here goes your title"/>
<textarea class="ckeditor" name="art_body" id="art_body"></textarea>
<input type="submit" name="savedraft" id="savedraft" onclick="saveArt();return false;" value="Need more Research ! Save as Draft" class="button"/>
</form>
like image 285
user3099225 Avatar asked May 22 '15 13:05

user3099225


2 Answers

You can force CKeditor to update the textarea value using:

for (instance in CKEDITOR.instances) {
    CKEDITOR.instances[instance].updateElement();
}

Also, you can use .serialize for the data - then you won't have to maintain the AJAX code if parameters change:

<script src="ckeditor/ckeditor.js"></script>
function saveArt() 
{
    for (instance in CKEDITOR.instances) {
        CKEDITOR.instances[instance].updateElement();
    }

    jQuery.ajax({
        type: 'POST',
        url: 'art_save.php',
        data: $("#art").serialize()
     });  
     return false; 

 }
like image 76
Carl Avatar answered Oct 23 '22 11:10

Carl


for (instance in CKEDITOR.instances) 
{
    CKEDITOR.instances[instance].updateElement();
}

$.ajax({
    url: "controllers/c.publicidad.php?task=save",
    type: 'post',
    data: $("form[name='form']").serialize(),
    dataType: 'json',
    success: function(data) {
        if(data.idenvio != ''){
            $("form[name='frmPublicidad']").toggle("slow");
            $("#MessageSuscripcion").html(data.message);
        }
    }
});
like image 4
Fiestas Sanchez Ivan Avatar answered Oct 23 '22 11:10

Fiestas Sanchez Ivan