JS-RichEdit and PHP / MySQL

Example

This is an example how you can use the richtext editor with PHP. Type something into the editor, and hit the submit button. The submitted content will appear below and in the editor.



Usage

You can access the submitted contents with PHP simply by using their corresponding field names:
$richEdit0, $richEdit1, $richEdit2, ...
If your PHP configuration does not register global variables (register_globals = Off), then you can access them like this (PHP 4.1.0 or higher):
$_REQUEST['richEdit0'], $_REQUEST['richEdit1'], $_REQUEST['richEdit2'], ...
BTW, the default field name richEdit can be changed in the configuration section of richedit.js or when creating the editor to anything you like.

Usually PHP adds slashes automatically to GPC data, so if you want to display the contents you should strip all slashes first:
if(get_magic_quotes_gpc()) $richEdit0 = stripslashes($richEdit0);
However, when you store the contents into a database, slashes must not be stripped; instead, if your PHP configuration does not add slashes to GPC data automatically, you should add them yourself or you might get a MySQL error:
if(!get_magic_quotes_gpc()) {
  $richEdit0 = addslashes($richEdit0);
  $richEdit1 = addslashes($richEdit1);
}
mysql_query("INSERT INTO table_name (field_name1, field_name2) VALUES ('$richEdit0', '$richEdit1')");
If you want to put content into the richtext editor with PHP, e.g. from a database, first you have to remove all CR and NL characters and add slashes. If you don't, you'll probably get a JavaScript error:
$content = preg_replace("/\r|\n/", '', $content);
if(!get_magic_quotes_runtime()) $content = addslashes($content);
Finally, it's a good idea to have a look at the source code of this page. ;-)