256 Kilobytes

Answers in Web Development | By Some Guy

Published 1 year agoSun, 13 Jan 2019 04:24:05 -0800

177 views, 0 RAMs, and 1 comment

Tags: Input Validation, CKEditor

Profile Photo - Some Guy Some Guy
🗎 3224 🗨 43 🐏 31
Subscriber

I just started using CKEditor (and I LOVE it), but I've been trying to figure out the best way to conduct server-side validation using it. Any tips would be greatly appreciated.

Download more RAM. 🐏 ⨉ 0 Posted by Some Guy 1 year ago 🕓 Posted at 13 January, 2019 04:24 AM PST

This is a patsy account used by the 256 Kilobytes staff to seed content. 

Profile Photo - August R. Garcia August R. Garcia LARPing as a Sysadmi... Portland, OR
🗎 213 🗨 1114 🐏 336
Site Owner

While the exact steps vary based on your server-side programming language, here's the short version.

1. How to Get Data from CKEditor in PHP (Or Other Languages)

As stated by the CKEditor documentation:

When CKEditor functions as a replacement for a <textarea> element, the integration with the parent <form> element is automatic. CKEditor automatically updates the replaced <textarea> when the form is submitted, so there is no need to change any server-side code handling form submission after enabling CKEditor on an exisiting form element.

This means that when submitting a form containing an editor instance, its data will simply be posted to the server, using the <textarea> element name as the key to retrieve it.

For example, for the <textarea> element with an ID of editor1, as used in our Quick Start Guide example, you could create this PHP code:

<?php
    $editor_data = $_POST[ 'editor1' ];
?>

This method works for any CKEditor instance that replaces a <textarea> in a <form> element, both classic and inline.

Source: Saving Data in CKEditor Replacing a Textarea

Basically, you can access the data server-side the same way as you would access the data from the textarea that CKEditor replaced.

2. Validating CKEditor Data on the Server Side

The exact validation will depend on what you want to do. A few basic examples expanding on the code from the documentation shown above:

<?php
// Example 1: If CKEditor Data Exists:
// https://www.tutorialspoint.com/php7/php7_coalescing_operator.htm
$editor_data = $_POST[ 'editor1' ] ?? NULL;

if ($editor_data) {
  /* Do Something */ 
}

// Example 2: Set a default if CKEditor Data does not exist:
// https://www.tutorialspoint.com/php7/php7_coalescing_operator.htm
$editor_data = $_POST[ 'editor1' ] ?? "<p>This Post Left Blank</p>";

/* Do Something */ 
?>
Download more RAM. 🐏 ⨉ 0 Posted by August R. Garcia 3 months ago 🕓 Posted at 03 December, 2019 13:05 PM PST

Sir, I can do you a nice SEO.

Post a New Comment

To leave a comment, login to your account or create an account.

Do you like having a good time?

Read Quality Articles

Read some quality articles. If you can manage to not get banned for like five minutes, you can even post your own articles.

View Articles →

Argue with People on the Internet

Use your account to explain why people are wrong on the Internet forum.

View Forum →

Vandalize the Wiki

Or don't. I'm not your dad.

View Wiki →

Ask and/or Answer Questions

If someone asks a terrible question, post a LMGTFY link.

View Answers →

Make Some Money

Hire freelancers and/or advertise your goods and/or services. Hire people directly. We're not a middleman or your dad. Manage your own business transactions.

Register an Account
You can also login to an existing account or recover your password. All use of this site is subject to terms outlined in the terms of service and privacy policy.