• We just launched and are currently in beta. Join us as we build and grow the community.

How to Add WYSIHTML5 Text Editor

Bigglez

Crypto Compliance Analyst
B Rep
0
0
0
Rep
0
B Vouches
0
0
0
Vouches
0
Posts
156
Likes
185
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
Getting Started

First, we need Bootstrap, jQuery and the WYSIHTML5 plugin that will provide our WYSIHTML5 editor which are included in the downloadable of this tutorial.

Creating our Database

Next, we create our database that will contain our posts.

I've included a SQL file in the downloadable of this tutorial. All you have to do is import the said file. If you have no idea on how to import, please visit my tutorial How import .sql file to restore MySQL database.

You should be able to create a database named db.

Displaying our Posts Table

Next, we are going to display the posts in our posts table from our database.

Create a new file, name it as index.php and paste the codes below.

  1. <?php

    session_start

    (

    )

    ;

    ?>
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <meta charset="utf-8">
  6. <title>How to Add WYSIHTML5 Text Editor</title>
  7. <link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.min.css">
  8. </head>
  9. <body>
  10. <div class="container">
  11. <h1 class="page-header text-center" style="margin-top:30px">Add WYSIHTML5 Text Editor</h1>
  12. <div class="row">
  13. <div class="col-sm-8 col-sm-offset-2">
  14. <?php
  15. if

    (

    isset

    (

    $_SESSION

    [

    'error'

    ]

    )

    )

    {
  16. echo

    "
  17. <div class='alert alert-danger text-center'>
  18. "

    .

    $_SESSION

    [

    'error'

    ]

    .

    "
  19. </div>
  20. "

    ;
  21. unset

    (

    $_SESSION

    [

    'error'

    ]

    )

    ;
  22. }

  23. if

    (

    isset

    (

    $_SESSION

    [

    'success'

    ]

    )

    )

    {
  24. echo

    "
  25. <div class='alert alert-success text-center'>
  26. "

    .

    $_SESSION

    [

    'success'

    ]

    .

    "
  27. </div>
  28. "

    ;
  29. unset

    (

    $_SESSION

    [

    'success'

    ]

    )

    ;
  30. }

  31. ?>
  32. <table class="table table-bordered">
  33. <thead>
  34. <th>ID</th>
  35. <th>Title</th>
  36. <th><a href="add.php" class="btn btn-primary btn-xs">Add New</a></th>
  37. </thead>
  38. <tbody>
  39. <?php
  40. //connection
  41. $conn

    =

    new

    mysqli(

    'localhost'

    ,

    'root'

    ,

    ''

    ,

    'db'

    )

    ;

  42. $sql

    =

    "SELECT * FROM posts"

    ;
  43. $query

    =

    $conn

    ->

    query

    (

    $sql

    )

    ;

  44. while

    (

    $row

    =

    $query

    ->

    fetch_assoc

    (

    )

    )

    {
  45. echo

    "
  46. <tr>
  47. <td>"

    .

    $row

    [

    'id'

    ]

    .

    "</td>
  48. <td>"

    .

    $row

    [

    'title'

    ]

    .

    "</td>
  49. <td><a href='view.php?id="

    .

    $row

    [

    'id'

    ]

    .

    "' class='btn btn-info btn-sm'>View</a></td>
  50. </tr>
  51. "

    ;
  52. }
  53. ?>
  54. </tbody>
  55. </table>
  56. </div>
  57. </div>
  58. </div>

  59. </body>
  60. </html>

Creating our Post Form

Next, we create our post form where we integrate our text editor.

Create a new file, name it as add.php and paste the codes below.

  1. <?php

    session_start

    (

    )

    ;

    ?>
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <meta charset="utf-8">
  6. <title>How to Add WYSIHTML5 Text Editor</title>
  7. <link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.min.css">
  8. <link rel="stylesheet" type="text/css" href="bootstrap-wysihtml5/bootstrap3-wysihtml5.min.css">
  9. </head>
  10. <body>
  11. <div class="container">
  12. <h1 class="page-header text-center" style="margin-top:30px">Add WYSIHTML5 Text Editor</h1>
  13. <div class="row">
  14. <div class="col-sm-8 col-sm-offset-2">
  15. <?php
  16. if

    (

    isset

    (

    $_SESSION

    [

    'error'

    ]

    )

    )

    {
  17. echo

    "
  18. <div class='alert alert-danger text-center'>
  19. "

    .

    $_SESSION

    [

    'error'

    ]

    .

    "
  20. </div>
  21. "

    ;
  22. unset

    (

    $_SESSION

    [

    'error'

    ]

    )

    ;
  23. }

  24. ?>
  25. <form method="POST" action="post.php">
  26. <div class="form-group">
  27. <input type="text" id="title" name="title" class="form-control" placeholder="input title">
  28. </div>
  29. <div class="form-group">
  30. <textarea id="content" name="content" class="form-control" rows="10"></textarea>
  31. </div>
  32. <button type="submit" class="btn btn-primary" name="submit">Submit</button> <a href="index.php" class="btn btn-default"><span class="glyphicon glyphicon-arrow-left"></span> Back</a>
  33. </form>
  34. </div>
  35. </div>
  36. </div>

  37. <script src="jquery/jquery.min.js"></script>
  38. <script src="bootstrap/js/bootstrap.min.js"></script>
  39. <script src="bootstrap-wysihtml5/bootstrap3-wysihtml5.all.min.js"></script>
  40. <script type="text/javascript">
  41. $(function () {
  42. //Add text editor
  43. $("#content").wysihtml5();
  44. })
  45. </script>
  46. </body>
  47. </html>

Creating our Add Post Script

Next, we create the script that adds our post into our database.

Create a new file, name it as post.php and paste the codes below.

  1. <?php
  2. session_start

    (

    )

    ;

  3. if

    (

    isset

    (

    $_POST

    [

    'submit'

    ]

    )

    )

    {
  4. //connection
  5. $conn

    =

    new

    mysqli(

    'localhost'

    ,

    'root'

    ,

    ''

    ,

    'db'

    )

    ;

  6. //get post values
  7. $title

    =

    $_POST

    [

    'title'

    ]

    ;
  8. $content

    =

    $_POST

    [

    'content'

    ]

    ;

  9. //insert post to database
  10. $sql

    =

    "INSERT INTO posts (title, post_text) VALUES ('$title

    ', '$content

    ')"

    ;
  11. if

    (

    $conn

    ->

    query

    (

    $sql

    )

    )

    {
  12. $_SESSION

    [

    'success'

    ]

    =

    'Post added successfully'

    ;
  13. header

    (

    'location: index.php'

    )

    ;
  14. }
  15. else

    {
  16. $_SESSION

    [

    'error'

    ]

    =

    'Cannot add post'

    ;
  17. header

    (

    'location: add.php'

    )

    ;
  18. }

  19. }
  20. else

    {
  21. $_SESSION

    [

    'error'

    ]

    =

    'Please fill up post form first'

    ;
  22. header

    (

    'location: index.php'

    )

    ;
  23. }


  24. ?>

Creating our View Post

Lastly, we create the page where we can view individual posts.

Create a new file, name it as view.php and paste the codes below.

  1. <?php
  2. //get the id
  3. $id

    =

    $_GET

    [

    'id'

    ]

    ;

  4. //get the row with the id
  5. $conn

    =

    new

    mysqli(

    'localhost'

    ,

    'root'

    ,

    ''

    ,

    'db'

    )

    ;
  6. $sql

    =

    "SELECT * FROM posts WHERE id = '$id

    '"

    ;
  7. $query

    =

    $conn

    ->

    query

    (

    $sql

    )

    ;
  8. $row

    =

    $query

    ->

    fetch_assoc

    (

    )

    ;
  9. ?>
  10. <!DOCTYPE html>
  11. <html>
  12. <head>
  13. <meta charset="utf-8">
  14. <title>How to Add WYSIHTML5 Text Editor</title>
  15. <link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.min.css">
  16. </head>
  17. <body>
  18. <div class="container">
  19. <h1 class="text-center" style="margin-top:30px">Add WYSIHTML5 Text Editor</h1>
  20. <hr>
  21. <div class="row">
  22. <div class="col-sm-8 col-sm-offset-2">
  23. <h4><b>TITLE</b>: <?php

    echo

    $row

    [

    'title'

    ]

    ;

    ?>

    </h4>
  24. <?php

    echo

    $row

    [

    'post_text'

    ]

    ;

    ?>
  25. </div>
  26. </div>
  27. </div>

  28. </body>
  29. </html>

That ends this tutorial. Happy Coding :)


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

452,496

331,422

331,430

Top