Creating a Simple Search Box using PHP

Learn on how to create a Simple Search Box using PHP

In this tutorial we will create a Simple Search Box 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 newly coders for its user-friendly environment.

So Let's do the coding...

Getting Started:

First, you have to download & install XAMPP or any local server that can runs 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_box. After that, click Import then locate the database file inside the folder of the application then click ok.

tut1

Or, you can also create the sample table and insert a sample data in your database by copying and pasting the code below at SQL tab in your PHPMyAdmin.

  1. CREATE TABLE `blog` (
  2. `blog_id` int(11) NOT NULL PRIMARY KEY AUTO_INCRMENT,
  3. `title` varchar(50) NOT NULL,
  4. `content` text NOT NULL
  5.  
  6. INSERT INTO `blog` (`blog_id`, `title`, `content`) VALUES
  7. (1, 'The Little Gingerbread Man', 'Once upon a time there was an old woman who loved baking gingerbread. She would bake gingerbread cookies, cakes, houses and gingerbread people, all decorated with chocolate and peppermint, caramel candies and colored frosting.'),
  8. (2, 'The Halloween House', 'Now Suzie\'s moved in--she\'s only 4--along with her brother, her father and mother, and little Picador. He\'s their dog. Well, maybe half a dog. He\'s a Chihuahua, as small as they come.\r\n\r\nSuzie\'s room is in the attic. It\'s no fun. With a high ceiling, cold and gloomy, and shadows that run halfway up the walls. Suzie hides under the blanket. Picador too. Come on, he\'s no guard dog.');

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 = mysqli_connect('localhost', 'root', '', 'db_box') or die(mysqli_error());
  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"/>
  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 - Simple Search Box</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="col-md-1"></div>
  18. <div class="col-md-10">
  19. <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#form_modal">Add Content</button>
  20. <br />
  21. <br />
  22. <form class="form-inline" method="POST" action="index.php">
  23. <div class="input-group col-md-12">
  24. <input type="text" class="form-control" placeholder="Search here..." name="keyword" required="required" value="<?php echo isset($_POST['keyword']) ? $_POST['keyword'] : '' ?>"/>
  25. <span class="input-group-btn">
  26. <button class="btn btn-primary" name="search"><span class="glyphicon glyphicon-search"></span></button>
  27. </span>
  28. </div>
  29. </form>
  30. <br />
  31. <?php
  32. if(ISSET($_POST['search'])){
  33. $keyword = $_POST['keyword'];
  34. ?>
  35. <div>
  36. <h2>Result</h2>
  37. <hr style="border-top:2px dotted #ccc;"/>
  38. <?php
  39. require 'conn.php';
  40. $query = mysqli_query($conn, "SELECT * FROM `blog` WHERE `title` LIKE '%$keyword%' ORDER BY `title`") or die(mysqli_error());
  41. while($fetch = mysqli_fetch_array($query)){
  42. ?>
  43. <div style="word-wrap:break-word;">
  44. <a href="get_blog.php?id=<?php echo $fetch['blog_id']?>"><h4><?php echo $fetch['title']?></h4></a>
  45. <p><?php echo substr($fetch['content'], 0, 100)?>...</p>
  46. </div>
  47. <hr style="border-bottom:1px solid #ccc;"/>
  48. <?php
  49. }
  50. ?>
  51. </div>
  52. <?php
  53. }
  54. ?>
  55. </div>
  56. </div>
  57. <div class="modal fade" id="form_modal" tabindex="-1" role="dialog" aria-hidden="true">
  58. <div class="modal-dialog">
  59. <form action="save_content.php" method="POST" enctype="multipart/form-data">
  60. <div class="modal-content">
  61. <div class="modal-body">
  62. <div class="col-md-2"></div>
  63. <div class="col-md-8">
  64. <div class="form-group">
  65. <label>Title</label>
  66. <input type="text" class="form-control" name="title" required="required"/>
  67. </div>
  68. <div class="form-group">
  69. <label>Content</label>
  70. <textarea class="form-control" style="resize:none; height:250px;" name="content" required="required"></textarea>
  71. </div>
  72. </div>
  73. </div>
  74. <div style="clear:both;"></div>
  75. <div class="modal-footer">
  76. <button type="button" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  77. <button name="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Save</button>
  78. </div>
  79. </div>
  80. </form>
  81. </div>
  82. </div>
  83. <script src="js/jquery-3.2.1.min.js"></script>
  84. <script src="js/bootstrap.js"></script>
  85. </body>
  86. </html>

Creating Save Query

This code contains the save query of the application. This code will save the data to the database server. To do that just copy and write this block of codes inside the text editor, then save it as save_content.php.

  1. <?php
  2. require_once 'conn.php';
  3.  
  4. if(ISSET($_POST['save'])){
  5. $title = addslashes($_POST['title']);
  6. $content = addslashes($_POST['content']);
  7.  
  8. mysqli_query($conn, "INSERT INTO `blog` VALUES('', '$title', '$content')") or die(mysqli_error());
  9.  
  10. header('location: index.php');
  11.  
  12. }
  13. ?>

Creating the View Content

This code contains the view content of the application. This script will display the search keyword content that have been inputted. To make these just follow and write the code below inside the text editor, then save it as get_blog.php.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width"/>
  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 - Simple Search Box</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <a href="index.php" class="btn btn-success">Back</a>
  18. <?php
  19. require 'conn.php';
  20. if(ISSET($_REQUEST['id'])){
  21. $query = mysqli_query($conn, "SELECT * FROM `blog` WHERE `blog_id` = '$_REQUEST[id]'") or die(mysqli_error());
  22. $fetch = mysqli_fetch_array($query);
  23. ?>
  24. <h3><?php echo $fetch['title']?></h3>
  25. <p><?php echo nl2br($fetch['content'])?></p>
  26. <?php
  27. }
  28. ?>
  29.  
  30. </div>
  31. </body>
  32. </html>

Creating the Main Function

This code contains the main function of the application. This code will find and search the inputted keyword to check if it exist in the database To do this just copy and write the code inside the text editor, then save it as search.php.

  1. <?php
  2. if(ISSET($_POST['search'])){
  3. $keyword = $_POST['keyword'];
  4. ?>
  5. <div>
  6. <h2>Result</h2>
  7. <hr style="border-top:2px dotted #ccc;"/>
  8. <?php
  9. require 'conn.php';
  10. $query = mysqli_query($conn, "SELECT * FROM `blog` WHERE `title` LIKE '%$keyword%' ORDER BY `title`") or die(mysqli_error());
  11. while($fetch = mysqli_fetch_array($query)){
  12. ?>
  13. <div style="word-wrap:break-word;">
  14. <a href="get_blog.php?id=<?php echo $fetch['blog_id']?>"><h4><?php echo $fetch['title']?></h4></a>
  15. <p><?php echo substr($fetch['content'], 0, 100)?>...</p>
  16. </div>
  17. <hr style="border-bottom:1px solid #ccc;"/>
  18. <?php
  19. }
  20. ?>
  21. </div>
  22. <?php
  23. }
  24. ?>

Demo

There you have it we successfully created Simple Search Box using PHP. I hope that this simple tutorial helps you to what you are looking for. For more updates and tutorials just kindly visit this site.

Enjoy Coding!

Comments

Submitted byAnonymous (not verified)on Fri, 01/29/2021 - 18:30

good
Submitted bygood too (not verified)on Mon, 11/20/2023 - 23:21

In reply to by Anonymous (not verified)

good too
Submitted byManish Chander (not verified)on Thu, 08/26/2021 - 13:17

The tutorial is great. I have a query. How to make the search result link to open on modal and show the brief blog inside a modal rather than showing it on new page (get_blog.php). Now when you click on the link it navigates to get_blog.php page. How to get it opened inside a modal ? Bcoz if there are 5 results and if people clicks on the first and read it, If the click on back button the results vanishes. Again they have to search for the results. Please help

Add new comment