数据库配置(dbconfig.php)
“dbconfig.php”文件用于使用PHP和MySQL连接数据库。
指定数据库主机('$dbhost'),用户名('$dbusame'),密码('$dbpassword'),以及根据数据库凭据的名称('$dbname')。
<?php //Database configuration $dbHost = "localhost"; $dbUsername = "root"; $dbPassword = "root"; $dbName = "onitroad"; //Create database connection $db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName); //Check connection if ($db->connect_error) { die("Connection failed: " . $db->connect_error); }
保存数据库中HTML编辑器内容
提交表单后,编辑器的内容将发布到服务器端脚本('submit.php'),用于在数据库中插入HTML格式化内容。
- 使用PHP中的$_POST方法从发布的表单数据中检索编辑器内容。
- 使用PHP和MySQL在数据库中插入HTML内容。
- 向用户显示状态消息。
<?php //Include the database configuration file require_once 'dbConfig.php'; $editorContent = $statusMsg = ''; //If the form is submitted if(isset($_POST['submit'])){ //Get editor content $editorContent = $_POST['editor']; //Check whether the editor content is empty if(!empty($editorContent)){ //Insert editor content in the database $insert = $db->query("INSERT INTO editor (content, created) VALUES ('".$editorContent."', NOW())"); //If database insertion is successful if($insert){ $statusMsg = "The editor content has been inserted successfully."; }else{ $statusMsg = "Some problem occurred, please try again."; } }else{ $statusMsg = 'Please add content in the editor.'; } } ?>
创建数据库表
要存储HTML编辑器内容,需要在数据库中创建表。
以下SQL使用MySQL数据库中的某些基本字段创建“编辑器”表。
CREATE TABLE `editor` ( `id` int(11) NOT NULL AUTO_INCREMENT, `content` text COLLATE utf8_unicode_ci NOT NULL, `created` datetime NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
wysiwyg html编辑器表单
首先,使用TextArea输入字段创建HTML表单,以允许用户添加内容并提交数据。
<form method="post" action="submit.php"> <textarea name="editor" id="editor" rows="10" cols="80"> This is my textarea to be replaced with HTML editor. </textarea> <input type="submit" name="submit" value="SUBMIT"> </form>
要接受来自用户HTML内容,TextArea元素需要在WysiWyg编辑器中转换。
我们可以使用任何WYSIWYG编辑器插件(CKEDITOR或者TINYMCE)将HTML编辑器添加到Textarea输入字段。
使用CKEDITOR添加WYSIWYG编辑:
包括CKEditor插件的JS库文件。
<script src="ckeditor/ckeditor.js"></script>
使用'ckeditor.replace()'方法用wysiwyg编辑器替换textarea字段。
<script> CKEDITOR.replace('editor'); </script>
添加Wysiwyg编辑器:
包括TinyMCE编辑器插件的JS库文件。
<script src="tinymce/tinymce.min.js"></script>
使用'tinymce.init()'方法用wysiwyg编辑器替换textarea字段。
<script> tinymce.init({ selector: '#editor' }); </script>
WYSIWYG编辑器允许用户在输入字段中插入HTML格式化文本。
我们可以使用jQuery插件轻松地将Wysiwyg HTML编辑器添加到Textarea。
有许多jQuery插件可用于将WysiWyg编辑器添加到Texarea。
CKEditor和TinyMCE编辑器是最流行的插件,用于在网页上添加丰富的文本编辑器。
提交WYSIWYG编辑器内容后,需要存储值以供以后使用。
通常,在Web应用程序中,数据库用于存储输入内容。
要保存WYSIWYG编辑器内容,需要将输入HTML值插入数据库中。
PHP $_POST方法是获得HTML编辑器值的最简单方法,并在数据库中保存WYSIWYG编辑器内容。
在本教程中,我们将展示如何使用PHP和MySQL在数据库中插入和保存WysiWyg编辑器HTML内容。