2013-03-05 15 views
7

Widziałem wiele postów na temat TinyMCE i Ajax, ale nie mogłem dostać pracy - nie poprzez wyzwalanie tinyMCE.triggerSave(); nie nadrzędnymi przedstawienia, a nie przez wyzwalanie tinyMCE.get("elm1").save();tinyMCE ajax, aby zapisać dane

Oto mój kod:

$('#ss').submit(function (e) { 
     e.preventDefault(); 
     var ed = tinyMCE.get('elm1'); 
     var data = ed.getContent() 

    // tinyMCE.triggerSave(); 
    // tinyMCE.get("elm1").save(); 

    // var data = $(this).serialize(); 
     console.log(data); 
     $.ajax({ 
      type:  'POST', 
      cache:  false, 
      url:  'xtras/ssheetsave.php', 
      data:  data, 
      success: function(){ 
         console.log("Updates have successfully been ajaxed"); 
      } 
     }); 
     return false; 
    }); 

forma:

<form id="ss" method="post" style="margin-top: 40px;"> 
    <input type="hidden" id="cat_id" name="cat_id" value="<?php echo $id; ?>"> 
    <textarea id="elm1" name="elm1" rows="15" cols="80" style="width: 80%"> 
       </textarea> 

    <input type="submit" name="save" value="Submit" /> 
    <input type="reset" name="reset" value="Reset" /> 
</form> 

i tinyMCE:

tinyMCE.init({ 
     // General options 
     mode : "textareas", 
     theme : "advanced", 
     plugins : "autolink,lists,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,wordcount,advlist,autosave,visualblocks", 
//  save_onsavecallback : "ajaxSave", 

     // Theme options 
     theme_advanced_buttons1 : "save,cancel,|,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect,|,forecolor,backcolor", 
     theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,|,undo,redo,|,cleanup,help,code,tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,advhr", 
     theme_advanced_toolbar_location : "top", 
     theme_advanced_toolbar_align : "left", 
     theme_advanced_statusbar_location : "bottom", 
     theme_advanced_resizing : true, 

     // Style formats 
     style_formats : [ 
      {title : 'Bold text', inline : 'b'}, 
      {title : 'Red text', inline : 'span', styles : {color : '#ff0000'}}, 
      {title : 'Red header', block : 'h1', styles : {color : '#ff0000'}}, 
      {title : 'Example 1', inline : 'span', classes : 'example1'}, 
      {title : 'Example 2', inline : 'span', classes : 'example2'}, 
      {title : 'Table styles'}, 
      {title : 'Table row 1', selector : 'tr', classes : 'tablerow1'} 
     ], 

     // Replace values for the template plugin 
     template_replace_values : { 
      username : "Some User", 
      staffid : "991234" 
     } 
    }); 

Czy ktoś może mi pomóc, aby to działało? - w zasadzie nawet nie dostać dane w konsoli console.log(data);

Odpowiedz

3

Zasadniczo mam go pracy poprzez wprowadzenie on() słuchacza:

$(document).on('submit','#ss',function (e) { 
    e.preventDefault(); 
    var ed = tinyMCE.get('elm1'); 
    var data = ed.getContent(); 
    var cat_id = $('#cat_id').val(); 

    console.log(cat_id); 
    $.ajax({ 
     type:  'GET', 
     cache:  false, 
     url:  'xtras/ssheetsave.php', 
     data:  'data=' + escape(data) + '&cat_id=' + cat_id, 
     success: function(){ 
        $("#ss").remove(); 
        $("#output").html(data); 
     } 
    }); 
    return false; 
}); 
0

Trzeba użyć „elm1” zamiast „zawartości”

var ed = tinyMCE.get('elm1'); 
+0

przykro , dorsz e jest nieco niechlujny, ponieważ próbowałem tak wielu odmian ... z 'content' i' elem1' - nadal nie działa ... – Elen

+0

czy możesz mi powiedzieć, co twoja konsola mówi, kiedy dzwonisz tinymce.editors – Thariama

+0

wreszcie działa, wprowadzając funkcję 'on()'! '$ (document) .on ('submit', '# ss', function (e)'! – Elen

5

Musisz użyć wartości "nazwa" w oryginalnym polu tekstowym, a zawartość otrzymasz za pomocą "getContent()"

To jest prosto z ich opowiadanie.

<script> 
tinymce.init({ 
    selector: "textarea", 
    plugins: "save", 
    toolbar: "save", 
    save_enablewhendirty: true, 
    save_onsavecallback: function() { 
         // USE THIS IN YOUR AJAX CALL 
       alert(tinyMCE.get('ajax_text').getContent()); 
     } 
}); 
</script> 

<textarea name="ajax_text" /></textarea> 
0

Oto pełny przykład roboczych, z nieco różnych JS:

HTML:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 
<script src="https://tinymce.cachefly.net/4.1/tinymce.min.js"></script> 

<textarea id="prjOView" name="prjOView" rows="8" cols="73"></textarea> 
<button id="btnProjSave">Save</button> 

Uwaga: W moim przypadku to nie wystarczyło, aby ustawić tylko atrybut name= . Musiałem ustawić atrybuty name i id i przypisać obie te same wartości (jak pokazano powyżej).

JS/jQuery:

$(document).ready(function(){ 

    tinymce.init({ 
     selector: '#prjOView', 
     theme: 'modern', 
     width: 600, 
     height: 300, 
     plugins: [ 
      'advlist autolink link image lists charmap print preview hr anchor pagebreak spellchecker', 
      'searchreplace wordcount visualblocks visualchars code fullscreen insertdatetime media nonbreaking', 
      'save table contextmenu directionality emoticons template paste textcolor' 
     ], 
     content_css: 'css/content.css', 
     toolbar: 'insertfile undo redo | styleselect | sizeselect | bold italic | fontselect | fontsizeselect | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image | print preview media fullpage | forecolor backcolor emoticons' 
    }); //tinyMCE 

    $('#btnProjSave').click(function(){ 
     var mi = tinymce.get('prjOView').getContent(); 
     alert(mi); 

     //The data is now in var `mi`, use AJAX to save via PHP 
     $.ajax({ 
      type: 'post', 
      url: 'path_to_your_php_file.php', 
      data: 'tinydata=' +mi, 
      success:function(){ 
      } 
     }); 
    }); 

}); //END document.ready 

plik path_to_your_php_file.php:

<?php 
    var $tinydata = $_POST['tinydata']; 
    //the data is now in the above var. Do what you need to do. 

Dla niektórych podstawowych przykładów, jak używać AJAX, zobacz this post