Xajax 0.2: Tips and Tricks: FCKeditor






Xajax 0.2: Tips and Tricks: FCKeditor

Creating a FCKeditor enabled form:

$editor_dir="FCKeditor/";
include_once($editor_dir."fckeditor.php") ;

$sBasePath = $_SERVER['PHP_SELF'] ;
$sBasePath = substr( $sBasePath, 0, strpos( $sBasePath, "login.php" ) ) ;

$oFCKeditor = new FCKeditor('report_text') ;
$oFCKeditor->BasePath    = $sBasePath.$editor_dir;

$oFCKeditor->Value = stripslashes($report_text);

$report_text_editor= $oFCKeditor->CreateHtml();

$text.="
<FORM id=main_form action='javascript:void(null)' onsubmit='submit_form(this,Array(\"report_text\"))'>

$report_text_editor

<input type=submit value=' Save >> ' class=submit_button>

</FORM>
";

$objResponse->addScript("__FCKeditorNS = null;");
$objResponse->addScript("FCKeditorAPI = null;");

If there are no editor on the page: <FORM id=main_form action='javascript:void(null)' onsubmit='submit_form(this)'>.

If there are several editors: <FORM id=main_form action='javascript:void(null)' onsubmit='submit_form(this,Array(\"report_text\",\"other_field_name\"))'>


The javascript function used when submitting forms. rt_editors - array of editor names placed on the page (there may 0..n editors on the same page)

<script type="text/javascript"><!--
function submit_form(frm,rt_editors)
{
     if (arguments.length > 1) {
          for (var i = 0; i < rt_editors.length; i++)
          {
               var val = get_ed_text(rt_editors[i]);
               frm.elements[rt_editors[i]].value = val;
          }
     }
     xajax_process_form(xajax.getFormValues(frm));
     return false;
}
// --></script>

"xajax_process_form" - your xajax registered function for parsing the form.


The javascript function for getting innerHTML of editor referenced by field name

<script type="text/javascript"><!--
function get_ed_text(editor_name)
{
    var oEditor = FCKeditorAPI.GetInstance(editor_name) ;
    if (oEditor.EditorDocument.body.innerHTML) {
        // Uncomment the following line for a proper XHTML return
        // return oEditor.GetXHTML();
        return oEditor.EditorDocument.body.innerHTML;
    }
    else return '';
}
// --></script>