How to use Ckeditor in PHP?

Apr 05, 2022 . Admin



Hello dev,

I am going to explain you how to use ckeditor in php step by step. You will learn how to use ckeditor in php form.

This article will give you simple example of how to apply ckeditor in php. We will use get simple how to add ckeditor in php code.

I will give you simple Example of how to use ckeditor with php.

So, let's see bellow solution:

config.php
<?php
    $host = "localhost";
    $user = "root";
    $password = "";
    $dbname = "demos";
    $con = mysqli_connect($host, $user, $password,$dbname);

    if (!$con) {
        die("Connection failed: " . mysqli_connect_error());
    }
?>
index.php
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>How to use Ckeditor in PHP? - Mywebtuts.com</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/ckeditor/4.18.0/ckeditor.js" integrity="sha512-woYV6V3QV/oH8txWu19WqPPEtGu+dXM87N9YXP6ocsbCAH1Au9WDZ15cnk62n6/tVOmOo0rIYwx05raKdA4qyQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
</head>
<body>
    <?php 
        include "config.php";

        if(isset($_POST['submit'])){
        
            $title = $_POST['title'];
            $short_desc = $_POST['short_desc'];
            $long_desc = $_POST['long_desc'];

            if($title != ''){
                    
                mysqli_query($con, "INSERT INTO contents(title,short_desc,long_desc) VALUES('".$title."','".$short_desc."','".$long_desc."') ");
                header('location: index.php');
            }
        }
    ?>
    <div class="container mt-4">
        <div class="row">
            <div class="col-md-12">
                <div class="card">
                    <div class="card-header text-center bg-primary text-white">
                        <h4>How to use Ckeditor in PHP? - Mywebtuts.com</h4>
                    </div>
                    <div class="card-body">
                        <form method='post' action=''>
                            <div class="mb-3">
                                <label for="Title"><strong>Title :</strong></label>
                                <input type="text" name="title" class="form-control">
                            </div>
                             <div class="mb-3">
                                <label for="Description"><strong>Short Description :</strong></label>
                                <textarea id='short_desc' name='short_desc' class="form-control"></textarea>
                             </div>
                             <div class="mb-3">
                                <label for="long_desc"><strong>Long Description :</strong></label>
                                <textarea id='long_desc' name='long_desc' ></textarea><br>
                             </div>
                             <div class="d-flex justify-content-center">
                                <input type="submit" name="submit" value="Submit" class="btn btn-success">
                             </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <!-- Script -->
    <script type="text/javascript">
        CKEDITOR.replace('long_desc',{
            height: "200px"
        }); 
    </script>
</body>
</html>
Output:

It will help you...
#PHP