YUI Library Home

Site Search (YDN & YUIBlog):

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

# Rich Text Editor: 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: '600px', 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: '600px', 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'); }); })();

## Configuration for This Example

You can load the necessary JavaScript and CSS for this example from Yahoo's servers. Click here to load the YUI Dependency Configurator with all of this example's dependencies preconfigured.

### 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 Examples:

- Flickr Image Search
- Calendar Plugin
- Icon Insertion
- Plain Text Switcher
- Editor Data Post with Connection Manager
- Skinning the Editor
- Default Toolbar Config
- Code Editor
- Editor inside a Tabview Control
- One Editor, Multiple Edit Areas
- Editor with Custom Image Browser
- Simple Editor — Basic Buttons
- Simple Editor — Advanced Buttons
- Editor — Basic Buttons
- Editor Auto Adjusting Height
- Editor in a Dialog Control
- Custom resizing for the Rich Text Editor (included with examples for Resize Utility)
- Simple Application (included with examples for Layout Manager)
- Complex Application (included with examples for Layout Manager)

### More Rich Text Editor Resources:

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

Copyright © 2010 Yahoo! Inc. All rights reserved.

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