Yahoo! Developer Network Home - Help

Site Search (YDN & YUIBlog):

# YUI Library Examples: Rich Text Editor (beta): Editor Data Post with Connection Manager

# Rich Text Editor (beta): Editor Data Post with Connection Manager

This example demonstrates how to use Connection Manager to post data to the server, filter it and return it to the Editor.

View example in new window.

This is some more test text.<br>This is some more test text.<br>This is some more test text.<br>This is some more test text.<br>This is some more test text.<br>This is some more test text.<br>This is some more test text.<br> Filter editor data on server.

## Setting up the Editors HTML

Setting up the Editor's HTML is done by creating a `textarea` control on the page.

<form method="post" action="#" id="form1"> <textarea id="editor" name="editor" rows="20" cols="75"> This is some more test text.<br>This is some more test text.<br>This is some more test text.<br> This is some more test text.<br>This is some more test text.<br>This is some more test text. </textarea> </form>

## Setting up the Editors Javascript

Once the `textarea` is on the page, then initialize the Editor like this:

(function() { //Setup some private variables var Dom = YAHOO.util.Dom, Event = YAHOO.util.Event, status = null; //The Editor config var myConfig = { height: '300px', width: '530px', animate: true, dompath: true, focusAtStart: true }; //Now let's load the Editor.. var myEditor = new YAHOO.widget.Editor('editor', myConfig); myEditor.render(); })();

## Setup the Connection Manager Object

We need to setup the callback object for Connection Manager. In the `handleSuccess` function, we will `eval` the data returned from the server.

Then we will call `myEditor.setEditorHTML` with the new HTML.

var handleSuccess = function(o) { YAHOO.log('Post success', 'info', 'example'); var json = o.responseText.substring(o.responseText.indexOf('{'), o.responseText.lastIndexOf('}') + 1); var data = eval('(' + json + ')'); status.innerHTML = 'Status: ' + data.Results.status + '<br>Filter: ' + data.Results.filter + '<br>' + (new Date().toString()); myEditor.setEditorHTML(data.Results.data); } var handleFailure = function(o) { YAHOO.log('Post failed', 'info', 'example'); var json = o.responseText.substring(o.responseText.indexOf('{'), o.responseText.lastIndexOf('}') + 1); var data = eval('(' + json + ')'); status.innerHTML = 'Status: ' + data.Results.status + '<br>'; } var callback = { success: handleSuccess, failure: handleFailure };

## Setup the Button and the Checkbox

Using a Button control, we create the new Button and subscribe to the `click` event to begin the transaction.

Before making the request, we need to pull the HTML from the editor by calling the `myEditor.saveHTML` method. This will place the filtered HTML from the Editor back into the textarea.

Once we have that, we contruct our query string, and fire off the request.

Note: The request is wrapped in a short `setTimeout` to allow the browser to finish the cleanup calls that the Editor is making.

_button.on('click', function(ev) { YAHOO.log('Button clicked, initiate transaction', 'info', 'example'); Event.stopEvent(ev); myEditor.saveHTML(); window.setTimeout(function() { var sUrl = "assets/post.php"; var data = 'filter=' + ((Dom.get('filter').checked) ? 'yes' : 'no') + '&editor_data=' + encodeURIComponent(myEditor.get('textarea').value); var request = YAHOO.util.Connect.asyncRequest('POST', sUrl, callback, data); }, 200); });

## Snippet of server side PHP

On the server side, first we filter the HTML to remove harmful HTML elements. Then we are doing a simple text filter (in this case the Elmer Fudd filter) and adding a "tagline" to the bottom of the data.

Once our filtering is complete, we are returning the new data to the browser.

header('Content-type: application/json'); // Use Services_JSON require_once('JSON.php'); $json = new Services_JSON(); //Aggressive filtering... $allow_tags = array( 'b', 'strong', 'i', 'em', 'u', 'a', 'p', 'sup', 'sub', 'div', 'img', 'span', 'font', 'br', 'ul', 'ol', 'li' ); $filter = $_POST['filter']; $r_data = getRawEditorData('editor_data'); //Function defined in an include $e_data = strip_tags($r_data, '<'.implode('><', $allow_tags).'>'); if ($filter == 'yes') { // Replace the words: $EditorData = fudd($e_data); //See full example code in the downloaded files.. $EditorData .= '<br><br>--<br>Footer added on server side after filter'; } else { //Do some filtering here.. $EditorData = $e_data; } //Create the payload JSON object to deliver back to the browser.. $data = new stdclass(); $data->Results = new stdclass(); $data->Results->raw_data = $r_data; $data->Results->filter = $filter; $data->Results->status = 'OK'; $data->Results->data = $EditorData; echo($json->encode($data));

## Full Example Javascript Source

(function() { var Dom = YAHOO.util.Dom, Event = YAHOO.util.Event, status = null; var handleSuccess = function(o) { YAHOO.log('Post success', 'info', 'example'); var json = o.responseText.substring(o.responseText.indexOf('{'), o.responseText.lastIndexOf('}') + 1); var data = eval('(' + json + ')'); status.innerHTML = 'Status: ' + data.Results.status + '<br>Filter: ' + data.Results.filter + '<br>' + (new Date().toString()); myEditor.setEditorHTML(data.Results.data); } var handleFailure = function(o) { YAHOO.log('Post failed', 'info', 'example'); var json = o.responseText.substring(o.responseText.indexOf('{'), o.responseText.lastIndexOf('}') + 1); var data = eval('(' + json + ')'); status.innerHTML = 'Status: ' + data.Results.status + '<br>'; } var callback = { success: handleSuccess, failure: handleFailure }; YAHOO.log('Create Button Control (#toggleEditor)', 'info', 'example'); var _button = new YAHOO.widget.Button('submitEditor'); var myConfig = { height: '300px', width: '530px', animate: true, dompath: true }; YAHOO.log('Create the Editor..', 'info', 'example'); var myEditor = new YAHOO.widget.Editor('editor', myConfig); myEditor.render(); _button.on('click', function(ev) { YAHOO.log('Button clicked, initiate transaction', 'info', 'example'); Event.stopEvent(ev); myEditor.saveHTML(); window.setTimeout(function() { var sUrl = "assets/post.php"; var data = 'filter=' + ((Dom.get('filter').checked) ? 'yes' : 'no') + '&editor_data=' + encodeURIComponent(myEditor.get('textarea').value); var request = YAHOO.util.Connect.asyncRequest('POST', sUrl, callback, data); }, 200); }); Event.onDOMReady(function() { status = Dom.get('status'); }); })();

### YUI Logger Output:

Note: You are viewing this example in debug mode with logging enabled. This can significantly slow performance.

Reload with logging
and debugging disabled.

### Rich Text Editor (beta) Examples:

### More Rich Text Editor (beta) Resources:

- User's Guide (external)
- API Documentation
- Cheat Sheet PDF (external)

Copyright © 2008 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings