Managing Page Content using PHP and Summernote Text Editor Tutorial
This is simple Tutorial Code that tackles about Managing Page Content on a Web Application. This tutorial will help you to understand and have an idea of how to create a Page Content Management Feature for a CMS Website. This kind of feature can give your end-users a way to create or manage page content through the front-end or without managing the actual source code files. This can be useful for those who are planning to develop a Content Management System (CMS) using PHP Language.
Below, I provided a source code for a simple web-based application where end users can created a new page content. The source code below uses the following libraries:
Since we will be using PHP Language for our back-end codes, kindly download and install also a local web server such as XAMPP to run the PHP Scripts.
Creating the Interfaces
The below scripts are the PHP Files which contains the HTML Scripts for the interface and PHP Scripts. Save the following files according to the filename written above each scripts.
This file is the main page of the application which list all the created page content.
- <?php session_start(); ?>
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <link rel="stylesheet" href="./css/bootstrap.min.css">
- <link rel="stylesheet" href="./summernote/summernote-lite.css">
- <style>
- :root {
- --bs-success-rgb: 71, 222, 152 !important;
- }
- html,
- body {
- height: 100%;
- width: 100%;
- font-family: Apple Chancery, cursive;
- }
- .btn-info.text-light:hover,.btn-info.text-light:focus{
- background: #000;
- }
- </style>
- </head>
- <body class="bg-light">
- <nav class="navbar navbar-expand-lg navbar-dark bg-primary bg-gradient" id="topNavBar">
- <div class="container">
- <a class="navbar-brand" href="https://sourcecodester.com">
- Sourcecodester
- </a>
- </div>
- </nav>
- <div class="container py-3" id="page-container">
- <hr>
- <?php
- if(isset($_SESSION['msg'])):
- ?>
- <div class="alert alert-<?php echo $_SESSION['msg']['type'] ?>">
- <div class="d-flex w-100">
- </div>
- </div>
- <?php
- unset($_SESSION['msg']);
- endif;
- ?>
- <div class="col-12 my-2">
- </div>
- <div class="row row-cols-sm-1 row-cols-md-3 row-cols-xl-4 gx-4 gy-2">
- <?php
- $pages = scandir('./pages');
- asort($pages);
- foreach($pages as $page):
- if(in_array($page,array('.','..')))
- continue;
- ?>
- <div class="col">
- <div class="card border-right border-primary rounded-0">
- <div class="card-body">
- <div class="w-100 d-flex justify-content-end">
- </div>
- </div>
- </div>
- </div>
- <?php endforeach; ?>
- </div>
- </div>
- <script>
- $(function(){
- $('.delete_data').click(function(e){
- e.preventDefault()
- var _conf = confirm("Are you sure to delete this page content?")
- if(_conf ===true){
- location.href=$(this).attr('href')
- }
- })
- })
- </script>
- </body>
- </html>
This file is the interface for the page content form. This is the page where the users create new and update page content file.
- <?php session_start(); ?>
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <link rel="stylesheet" href="./css/bootstrap.min.css">
- <link rel="stylesheet" href="./summernote/summernote-lite.css">
- <style>
- :root {
- --bs-success-rgb: 71, 222, 152 !important;
- }
- html,
- body {
- height: 100%;
- width: 100%;
- font-family: Apple Chancery, cursive;
- }
- input.form-control.border-0{
- transition:border .3s linear
- }
- input.form-control.border-0:focus{
- box-shadow:unset !important;
- border-color:var(--bs-info) !important
- }
- .note-editor.note-frame .note-editing-area .note-editable, .note-editor.note-airframe .note-editing-area .note-editable {
- background: var(--bs-white);
- }
- </style>
- </head>
- <body class="bg-light">
- <nav class="navbar navbar-expand-lg navbar-dark bg-primary bg-gradient" id="topNavBar">
- <div class="container">
- <a class="navbar-brand" href="https://sourcecodester.com">
- Sourcecodester
- </a>
- </div>
- </nav>
- <div class="container py-3" id="page-container">
- <?php
- if(isset($_SESSION['msg'])):
- ?>
- <div class="alert alert-<?php echo $_SESSION['msg']['type'] ?>">
- <div class="d-flex w-100">
- </div>
- </div>
- <?php
- unset($_SESSION['msg']);
- endif;
- ?>
- <div class="card">
- <div class="card-header">
- Manage Page Content
- </div>
- <div class="card-body">
- <form action="save_page.php" id="content-form" method="POST">
- <input type="hidden" name="filename" value="<?php echo isset($_SESSION['POST']['filename']) ? $_SESSION['POST']['filename'] : (isset($_GET['page']) ? str_replace('.html','',$_GET['page']) : '') ?>">
- <div class="form-group col-6">
- <input type="text" pattern="[a-z0-9A-Z_-]+" name="fname" id="fname" autofocus autocomplete="off" class="form-control form-control-sm border-0 border-bottom rounded-0" value="<?php echo isset($_SESSION['POST']['fname']) ? $_SESSION['POST']['fname'] : (isset($_GET['page']) ? str_replace('.html','',$_GET['page']) : '') ?>" required>
- </div>
- <div class="form-group col-12">
- </div>
- </form>
- </div>
- <div class="card-footer">
- </div>
- </div>
- </div>
- </div>
- <script>
- $('.summernote').summernote({
- placeholder: 'Create you content here.',
- tabsize: 5,
- height: '50vh',
- toolbar: [
- ['style', ['style']],
- ['font', ['bold', 'underline', 'clear']],
- ['color', ['color']],
- ['para', ['ul', 'ol', 'paragraph']],
- ['table', ['table']],
- ['insert', ['link', 'picture', 'video']],
- ['view', ['fullscreen', 'codeview', 'help']]
- ]
- });
- </script>
- </body>
- </html>
This file is the page that displays the page contents created.
- <?php session_start(); ?>
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <link rel="stylesheet" href="./css/bootstrap.min.css">
- <link rel="stylesheet" href="./summernote/summernote-lite.css">
- <style>
- :root {
- --bs-success-rgb: 71, 222, 152 !important;
- }
- html,
- body {
- height: 100%;
- width: 100%;
- font-family: Apple Chancery, cursive;
- }
- .btn-info.text-light:hover,.btn-info.text-light:focus{
- background: #000;
- }
- </style>
- </head>
- <body class="bg-light">
- <nav class="navbar navbar-expand-lg navbar-dark bg-primary bg-gradient" id="topNavBar">
- <div class="container">
- <a class="navbar-brand" href="https://sourcecodester.com">
- Sourcecodester
- </a>
- </div>
- </nav>
- <div class="container py-3" id="page-container">
- <hr>
- <?php
- if(isset($_SESSION['msg'])):
- ?>
- <div class="alert alert-<?php echo $_SESSION['msg']['type'] ?>">
- <div class="d-flex w-100">
- </div>
- </div>
- <?php
- unset($_SESSION['msg']);
- endif;
- ?>
- <div class="col-12 my-2">
- </div>
- <div class="content">
- <?php echo isset($_GET['page']) && is_file("./pages/{$_GET['page']}") ? file_get_contents("./pages/{$_GET['page']}") : "<center>Unknown Page Content.</center>" ?>
- </div>
- </div>
- </body>
- </html>
Creating the PHP Scripts
The following scripts are the PHP files that contains the codes that saving,updating, and deleting the page content files. Save the following files according to the filename written above each scripts.
save_page.phpThis is the script for saving the new page content or updating the existing page content.
- <?php
- $current_name = $_POST['filename'];
- $new_name = $_POST['fname'];
- $content = $_POST['content'];
- $i = 0;
- if($current_name != $new_name){
- $nname = $new_name;
- while(true){
- $nname = $new_name."_".($i++);
- }else{
- break;
- }
- }
- $new_name = $nname;
- }
- }
- if($save > 0){
- $_SESSION['msg']['type'] = 'success';
- $_SESSION['msg']['text'] = 'Page Content Successfully Saved.';
- }else{
- $_SESSION['msg']['type'] = 'danger';
- $_SESSION['msg']['text'] = 'Page Content has failed to save.';
- $_SESSION['POST'] = $_POST;
- }
- ?>
This is the script for deleting the existing page content.
- <?php
- $page = $_GET['page'];
- if($delete){
- $_SESSION['msg']['type'] = 'success';
- $_SESSION['msg']['text'] = 'Page Content Successfully deleted.';
- }else{
- $_SESSION['msg']['type'] = 'danger';
- $_SESSION['msg']['text'] = 'Page Content has failed to delete.';
- }
- }else{
- $_SESSION['msg']['type'] = 'danger';
- $_SESSION['msg']['text'] = 'Page Content is unknown.';
- }
There you go. You can now test the web application on your end and see if it works like it was planned to. If you have encountered any errors, kindly review your source code with the source code I provided above. You may also download the working source code I created for this tutorial. The download is located below this article.
DEMO VIDEO
That's the end of this tutorial. I hope this simple PHP Tutorial will help you with what you are looking for and for your future CMS Projects.
Explore on this website for more Tutorials and Free Source Codes.
Happy Coding :)
Comments
Add new comment
- Add new comment
- 4290 views