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

PHP Simple Search using AJAX/Bootstrap

NEFTY

SEO Monetization Expert
N Rep
0
0
0
Rep
0
N Vouches
0
0
0
Vouches
0
Posts
170
Likes
131
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
In this tutorial, I'm going to show you how to create a simple search using ajax/bootstrap. It features a suggestion list from data in our database. Hope this will help you.

Creating our Database

First and most important step in to create our database.
1. Open phpMyAdmin.
2. Click databases, create a database and name it as "ajax_search".
3. After creating a database, click the SQL and paste the below code. See image below for detailed instruction.

  1. CREATE

    TABLE

    `user`

    (
  2. `userid`

    INT

    (

    11

    )

    NOT

    NULL

    AUTO_INCREMENT

    ,
  3. `firstname`

    VARCHAR

    (

    30

    )

    NOT

    NULL

    ,
  4. `lastname`

    VARCHAR

    (

    30

    )

    NOT

    NULL

    ,
  5. PRIMARY

    KEY

    (

    `userid`

    )
  6. )

    ENGINE=

    InnoDB DEFAULT

    CHARSET=

    latin1;

database_6_8.png

Inserting Sample Data into our Database

Next is to insert sample users into the database that we have created. We are going to use this in our search.

1. Click "ajax_search" database that we have created.
2. Click SQL and paste the code below.

  1. INSERT

    INTO

    `user`

    (

    `firstname`

    ,

    `lastname`

    )

    VALUES
  2. (

    'neovic'

    ,

    'devierte'

    )

    ,
  3. (

    'lee'

    ,

    'ann'

    )

    ,
  4. (

    'julyn'

    ,

    'divinagracia'

    )

    ,
  5. (

    'jaira'

    ,

    'jacinto'

    )

    ,
  6. (

    'tintin'

    ,

    'demapanag'

    )

    ,
  7. (

    'dee'

    ,

    'tolentino'

    )

    ,
  8. (

    'tintin'

    ,

    'devierte'

    )

    ;

Creating our Connection

Next step is to create a database connection and save it as "conn.php". This file will serve as our bridge between our form and our database. To create the file, open your HTML code editor and paste the code below after the tag.

  1. <?php

  2. //MySQLi Procedural
  3. $conn

    =

    mysqli_connect

    (

    "localhost"

    ,

    "root"

    ,

    ""

    ,

    "ajax_search"

    )

    ;
  4. if

    (

    !

    $conn

    )

    {
  5. die

    (

    "Connection failed: "

    .

    mysqli_connect_error

    (

    )

    )

    ;
  6. }

  7. ?>

Creating our Search Form

Next step is to create our search form. Also, take note that the css and scripts that I used in this tutorial are hosted so you might need internet for them to work. We name this as "index.php".

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>PHP Simple Search using AJAX/Bootstrap</title>
  5. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
  6. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
  7. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  8. <style>
  9. #result{
  10. position:absolute;
  11. top:45px;
  12. left:175px;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <nav class="navbar navbar-default">
  18. <div class="container-fluid">
  19. <div class="navbar-header">
  20. <a class="navbar-brand" href="https://www.sourcecodester.com/user/224918/track">nurhodelta_17</a>
  21. </div>

  22. <div class="collapse navbar-collapse navbar-ex1-collapse">
  23. <div class="col-sm-3 col-md-3">
  24. <form class="navbar-form" role="search" method="POST" action="result.php">
  25. <div class="input-group">
  26. <input type="text" class="form-control" placeholder="Search" name="search" id="search">
  27. <div class="input-group-btn">
  28. <button class="btn btn-default" type="submit"><i class="glyphicon glyphicon-search"></i></button>
  29. </div>
  30. </div>
  31. </form>
  32. </div>
  33. </div>
  34. </div>
  35. </nav>
  36. <div id="result"></div>
  37. <

    script type=

    "text/javascript"

    >
  38. $(

    document)

    .ready

    (

    function

    (

    )

    {

  39. $(

    "#search"

    )

    .keyup

    (

    function

    (

    )

    {
  40. var

    name =

    $(

    '#search'

    )

    .val

    (

    )

    ;
  41. if

    (

    name ==

    ""

    )

    {
  42. $(

    "#result"

    )

    .html

    (

    ""

    )

    ;
  43. }
  44. else

    {
  45. $.ajax

    (

    {
  46. type:

    "POST"

    ,
  47. url:

    "search.php"

    ,
  48. data:

    {
  49. search:

    name
  50. }

    ,
  51. success:

    function

    (

    html)

    {
  52. $(

    "#result"

    )

    .html

    (

    html)

    .show

    (

    )

    ;
  53. }
  54. }

    )

    ;
  55. }
  56. }

    )

    ;
  57. }

    )

    ;

  58. function

    fill(

    Value)

    {

  59. $(

    '#search'

    )

    .val

    (

    Value)

    ;
  60. $(

    '#result'

    )

    .hide

    (

    )

    ;

  61. }
  62. </

    script>
  63. </body>
  64. </html>

Creating our Search Code

Next, we create our search code. This code will be the one to fetch data from our database. We name this as "search.php".

  1. <?php

  2. include

    "conn.php"

    ;
  3. if

    (

    isset

    (

    $_POST

    [

    'search'

    ]

    )

    )

    {
  4. $search

    =

    $_POST

    [

    'search'

    ]

    ;

  5. $query

    =

    mysqli_query

    (

    $conn

    ,

    "select * from `user` where firstname like '%$search

    %' o

    r lastname like '%$search

    %'"

    )

    ;

  6. if

    (

    mysqli_num_rows

    (

    $query

    )

    ==

    0

    )

    {
  7. echo

    '<div class="panel panel-default" style="width:235px;">'

    ;
  8. ?>
  9. <span style="margin-left:13px;">No results found</span>
  10. <?php
  11. '</div>'

    ;
  12. }
  13. else

    {
  14. echo

    '<div class="panel panel-default" style="width:235px;">'

    ;
  15. while

    (

    $row

    =

    mysqli_fetch_array

    (

    $query

    )

    )

    {
  16. ?>

  17. <span>
  18. <a href="user.php?id=<?php

    echo

    $row

    [

    'userid'

    ]

    ;

    ?>

    " style="text-decoration:none; color:black; margin-left:13px;"><?php

    echo

    $row

    [

    'firstname'

    ]

    ;

    ?>

    <?php

    echo

    $row

    [

    'lastname'

    ]

    ;

    ?>

    </a>
  19. </span><br>

  20. <?php
  21. }
  22. '</div>'

    ;
  23. }
  24. }

  25. ?>

Creating our Result Page

Next step is to create our result page for our search form in case the user chooses to enter search text. We name this as "result.php".

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>PHP Simple Search using AJAX/Bootstrap</title>
  5. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
  6. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
  7. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  8. </head>
  9. <body>
  10. <nav class="navbar navbar-default">
  11. <div class="container-fluid">
  12. <div class="navbar-header">
  13. <a class="navbar-brand" href="https://www.sourcecodester.com/user/224918/track">nurhodelta_17</a>
  14. </div>
  15. </div>
  16. </nav>
  17. <div class="row" style="margin-left: 50px;">
  18. Search Results: <ul style="list-style-type:none;">
  19. <?php
  20. include

    (

    'conn.php'

    )

    ;
  21. $search

    =

    $_POST

    [

    'search'

    ]

    ;

  22. $query

    =

    mysqli_query

    (

    $conn

    ,

    "select * from `user` where firstname like '%$search

    %' o

    r lastname like '%$search

    %'"

    )

    ;
  23. if

    (

    mysqli_num_rows

    (

    $query

    )

    ==

    0

    )

    {
  24. echo

    '<li>No results found!</li>'

    ;
  25. }
  26. else

    {
  27. while

    (

    $row

    =

    mysqli_fetch_array

    (

    $query

    )

    )

    {
  28. ?>
  29. <li>
  30. <a href="user.php?id=<?php

    echo

    $row

    [

    'userid'

    ]

    ;

    ?>

    " style="margin-left:13px;"><?php

    echo

    $row

    [

    'firstname'

    ]

    ;

    ?>

    <?php

    echo

    $row

    [

    'lastname'

    ]

    ;

    ?>

    </a>
  31. </li>
  32. <?php
  33. }
  34. }
  35. ?>
  36. </ul>
  37. <br>
  38. <a href="index.php" class="btn btn-primary"><span class="glyphicon glyphicon-arrow-left"></span> Back</a>
  39. </div>
  40. </body>
  41. </html>

Creating our Output Page

Lastly, we create our output page. This page will output the results of our searches. We name this as "user.php".

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>PHP Simple Search using AJAX/Bootstrap</title>
  5. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
  6. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
  7. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  8. </head>
  9. <body>
  10. <nav class="navbar navbar-default">
  11. <div class="container-fluid">
  12. <div class="navbar-header">
  13. <a class="navbar-brand" href="https://www.sourcecodester.com/user/224918/track">nurhodelta_17</a>
  14. </div>
  15. </div>
  16. </nav>
  17. <div class="row" style="margin-left: 50px;">
  18. <?php
  19. include

    (

    'conn.php'

    )

    ;
  20. $id

    =

    $_GET

    [

    'id'

    ]

    ;
  21. $query

    =

    mysqli_query

    (

    $conn

    ,

    "select * from `user` where userid='$id

    '"

    )

    ;
  22. $row

    =

    mysqli_fetch_array

    (

    $query

    )

    ;

  23. echo

    'User: <strong>'

    .

    $row

    [

    'firstname'

    ]

    .

    ' '

    .

    $row

    [

    'lastname'

    ]

    .

    '</strong>'

    ;
  24. ?>
  25. <a href="index.php" class="btn btn-primary"><span class="glyphicon glyphicon-arrow-left"></span> Back</a>
  26. </div>
  27. </body>
  28. </html>


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

450,270

322,965

322,974

Top