PHP - Dynamic Comment With "Read more" Feature

In this tutorial we will create Dynamic Comment With "Read more" Feature using PHP. PHP is a server-side scripting language designed primarily for web development. Using PHP, you can let your user directly interact with the script and easily to learned its syntax. It is mostly used by a newly coders for its user friendly environment. So let's now do the coding...

Before we get started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html. And this is the link for the jquery that i used in this tutorial https://jquery.com/. Lastly, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Creating Database

Open your database web server then create a database name in it db_comment, after that click Import then locate the database file inside the folder of the application then click ok. tut1

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.
  1. <?php
  2.         $conn = new mysqli('localhost', 'root', '', 'db_comment');
  3.        
  4.         if(!$conn){
  5.                 die("Error: Failed to connect to database!");
  6.         }
  7. ?>

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <head>
  4.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1" />
  5.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6.         </head>
  7. <body>
  8.         <nav class="navbar navbar-default">
  9.                 <div class="container-fluid">
  10.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11.                 </div>
  12.         </nav>
  13.         <div class="col-md-3"></div>
  14.         <div class="col-md-6 well">
  15.                 <h3 class="text-primary">PHP - Dynamic Comment With "Read more" Feature</h3>
  16.                 <hr style="border-top:1px dotted #ccc;"/>
  17.                 <form>
  18.                         <div class="form-inline">
  19.                                 <label>Name</label>
  20.                                 <input type="text" id="name" class="form-control"/>
  21.                         </div>
  22.                         <br />
  23.                         <div class="form-inline">
  24.                                 <label>Comment</label>
  25.                                 <br />
  26.                                 <textarea id="comment" style="resize:none; width:300px; height:200px;"></textarea>
  27.                                 <br />
  28.                                 <div id="status"></div>
  29.                                 <button type="button" id="save" class="btn btn-primary form-control"><span class="glyphicon glyphicon-save"></span> Comment</button>
  30.                         </div>
  31.                 </form>
  32.                 <hr style="1px dashed #000"/>
  33.                 <div id="result"></div>
  34.         </div>
  35. </body>
  36. <script src="js/jquery-3.2.1.min.js"></script>
  37. <script src="js/script.js"></script>
  38. </html>

Creating PHP Query

This code contains the php query of the application. This code will sent the data inputs to the database server by sending ajax request method, and display the data back to the webpage. To do that just copy and write this block of codes inside the text editor, then save it as shown below. save_comment.php
  1. <?php
  2.         require_once 'conn.php';
  3.        
  4.         $name = $_POST['name'];
  5.         $comment = $_POST['comment'];
  6.        
  7.         $conn->query("INSERT INTO `user` VALUES('', '$name', '$comment')");
  8.        
  9.         echo "Data Inserted";
  10. ?>
get_data.php
  1. <?php
  2.         require_once 'conn.php';
  3.        
  4.         if(ISSET($_POST['res'])){
  5.                 $query = $conn->query("SELECT * FROM `user` ORDER BY `user_id` ASC");
  6.                 while($fetch  = $query->fetch_array()){
  7. ?>     
  8.                 <div class = "col-md-12 content" style = "word-wrap:break-word; background-color:#fff; padding:20px;">
  9.                 <?php echo '<h4 class = "text-danger">'.$fetch['name'].'</h4>';?>
  10.                 <hr style = "border-top:1px solid #000;"/>
  11.                 <?php
  12.                         echo substr($fetch['comment'], 0, 200);
  13.                        
  14.                         if(strlen($fetch['comment']) > 100){
  15.                                 echo "<span id=user_id".$fetch['user_id']." style='display:none;'>".substr($fetch['comment'], 200)."</span>";
  16.                                 echo "&nbsp;<span class='more' name='more' data-target='".$fetch['user_id']."' style='color:blue; cursor:pointer;'>More...</span>";
  17.                         }
  18.                 ?>
  19.                 </div>
  20.                 <br style = "clear:both;"/>
  21.                 <br />
  22. <?php
  23.                 }
  24.         }      
  25. ?>

Creating jQuery Script

This is where the code that uses ajax request been used. This code is consist of different functionalities it includes the sending data to the database, and extending the comment by clicking. To do this just copy and write these block of codes inside the text editor, then save it as script.js inside the js folder.
  1. $(document).ready(function(){  
  2.         displayData();
  3.        
  4.         $("#save").on('click', function(){
  5.                 var name = $('#name').val();
  6.                 var comment = $('#comment').val();
  7.                
  8.                 if(name == "" && comment == ""){
  9.                         $('#status').html("<label class='text-warning'>Please complete the required field!</label>");
  10.                 }else{
  11.                         $.ajax({
  12.                                 url: 'save_comment.php',
  13.                                 type: 'POST',
  14.                                 data: {name: name, comment: comment},
  15.                                 success: function(){
  16.                                         displayData();
  17.                                         $('#name').val('');
  18.                                         $('#comment').val('')
  19.                                 }
  20.                         });
  21.                 }
  22.         });
  23.        
  24.         function displayData(){
  25.                 $.ajax({
  26.                         url: 'get_data.php',
  27.                         type: 'POST',
  28.                         data: {res: 1},
  29.                         success: function(data){
  30.                                 $('#result').html(data);
  31.                         }
  32.                 });
  33.         }
  34.        
  35.         $(document).on('click', '.more', function(){
  36.                 var id = $(this).data('target');
  37.                 var target = $(this).attr('name');
  38.                
  39.                 if(target == "more"){
  40.                         $('#user_id'+id+'').show();
  41.                         $(this).text('Hide');
  42.                         $(this).attr('name', 'hide');
  43.                 }else{
  44.                         $('#user_id'+id+'').hide();
  45.                         $(this).text('More...');
  46.                         $(this).attr('name', 'more');
  47.                 }
  48.                
  49.         });
  50.        
  51. });
There you have it we successfully created Dynamic Comment With "Read more" Feature using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!!

Add new comment