• Register now to get access to thousands of Tutorials, Leaked content, Hot NSFW and much more. Join us as we build and grow the community.

Advertise Here

Advertise Here

Advertise Here

PHP - Dynamic Comment With "Read more" Feature

cvdmdkdd

Data-Driven Marketer
C Rep
0
0
0
Rep
0
C Vouches
0
0
0
Vouches
0
Posts
37
Likes
123
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 100 XP
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.
2018-08-03_13_55_33-localhost_127.0.0.1_db_comment_phpmyadmin_4.7.0.png

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. if

    (

    !

    $conn

    )

    {
  4. die

    (

    "Error: Failed to connect to database!"

    )

    ;
  5. }
  6. ?>

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>

    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>

    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. $name

    =

    $_POST

    [

    'name'

    ]

    ;
  4. $comment

    =

    $_POST

    [

    'comment'

    ]

    ;

  5. $conn

    ->

    query

    (

    "INSERT INTO `user` VALUES('', '$name

    ', '$comment

    ')"

    )

    ;

  6. echo

    "Data Inserted"

    ;
  7. ?>

get_data.php
  1. <?php
  2. require_once

    'conn.php'

    ;

  3. if

    (

    ISSET

    (

    $_POST

    [

    'res'

    ]

    )

    )

    {
  4. $query

    =

    $conn

    ->

    query

    (

    "SELECT * FROM `user` ORDER BY `user_id` ASC"

    )

    ;
  5. while

    (

    $fetch

    =

    $query

    ->

    fetch_array

    (

    )

    )

    {
  6. ?>
  7. <div class = "col-md-12 content" style = "word-wrap:break-word; background-color:#fff; padding:20px;">
  8. <?php

    echo

    '<h4 class = "text-danger">'

    .

    $fetch

    [

    'name'

    ]

    .

    '</h4>'

    ;

    ?>
  9. <hr style = "border-top:1px solid #000;"/>
  10. <?php
  11. echo

    substr

    (

    $fetch

    [

    'comment'

    ]

    ,

    0

    ,

    200

    )

    ;

  12. if

    (

    strlen

    (

    $fetch

    [

    'comment'

    ]

    )

    >

    100

    )

    {
  13. echo

    "<span id=user_id"

    .

    $fetch

    [

    'user_id'

    ]

    .

    " style='display:none;'>"

    .

    substr

    (

    $fetch

    [

    'comment'

    ]

    ,

    200

    )

    .

    "</span>"

    ;
  14. echo

    "&nbsp;<span class='more' name='more' data-target='"

    .

    $fetch

    [

    'user_id'

    ]

    .

    "' style='color:blue; cursor:pointer;'>More...</span>"

    ;
  15. }
  16. ?>
  17. </div>
  18. <br style = "clear:both;"/>
  19. <br />
  20. <?php
  21. }
  22. }
  23. ?>

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. $(

    "#save"

    )

    .on

    (

    'click'

    ,

    function

    (

    )

    {
  4. var

    name =

    $(

    '#name'

    )

    .val

    (

    )

    ;
  5. var

    comment =

    $(

    '#comment'

    )

    .val

    (

    )

    ;

  6. if

    (

    name ==

    ""

    &&

    comment ==

    ""

    )

    {
  7. $(

    '#status'

    )

    .html

    (

    "<label class='text-warning'>Please complete the required field!</label>"

    )

    ;
  8. }

    else

    {
  9. $.ajax

    (

    {
  10. url:

    'save_comment.php'

    ,
  11. type:

    'POST'

    ,
  12. data:

    {

    name:

    name,

    comment:

    comment}

    ,
  13. success:

    function

    (

    )

    {
  14. displayData(

    )

    ;
  15. $(

    '#name'

    )

    .val

    (

    ''

    )

    ;
  16. $(

    '#comment'

    )

    .val

    (

    ''

    )
  17. }
  18. }

    )

    ;
  19. }
  20. }

    )

    ;

  21. function

    displayData(

    )

    {
  22. $.ajax

    (

    {
  23. url:

    'get_data.php'

    ,
  24. type:

    'POST'

    ,
  25. data:

    {

    res:

    1

    }

    ,
  26. success:

    function

    (

    data)

    {
  27. $(

    '#result'

    )

    .html

    (

    data)

    ;
  28. }
  29. }

    )

    ;
  30. }

  31. $(

    document)

    .on

    (

    'click'

    ,

    '.more'

    ,

    function

    (

    )

    {
  32. var

    id =

    $(

    this

    )

    .data

    (

    'target'

    )

    ;
  33. var

    target =

    $(

    this

    )

    .attr

    (

    'name'

    )

    ;

  34. if

    (

    target ==

    "more"

    )

    {
  35. $(

    '#user_id'

    +

    id+

    ''

    )

    .show

    (

    )

    ;
  36. $(

    this

    )

    .text

    (

    'Hide'

    )

    ;
  37. $(

    this

    )

    .attr

    (

    'name'

    ,

    'hide'

    )

    ;
  38. }

    else

    {
  39. $(

    '#user_id'

    +

    id+

    ''

    )

    .hide

    (

    )

    ;
  40. $(

    this

    )

    .text

    (

    'More...'

    )

    ;
  41. $(

    this

    )

    .attr

    (

    'name'

    ,

    'more'

    )

    ;
  42. }

  43. }

    )

    ;

  44. }

    )

    ;

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!!!


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

Create an account or login to comment

You must be a member in order to leave a comment

Create account

Create an account on our community. It's easy!

Log in

Already have an account? Log in here.

452,501

355,782

355,789

Top