• 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 - Auto Search Using jQuery

Satan67

Exploit Kit Developer
S Rep
0
0
0
Rep
0
S Vouches
0
0
0
Vouches
0
Posts
163
Likes
149
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 400 XP
In this tutorial we will create a Auto Search using PHP. This code will auto search a specific data in the MySQLi server when the user entered a keyword. The code use jQuery ajax to automatically search a table row data from MySQLi server without refreshing web page and modify HTML table to display the result. This is a user-friendly kind of program feel free to modify it and use it as your own.

We will be using jQuery a JavaScript framework that design to simplify HTML DOM tree traversal and manipulation. It can retrieve DOM and manipulate each properties of an element based on different criteria such as id, class, etc.
.
Getting 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 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_auto, after that click Import then locate the database file inside the folder of the application then click ok.
2019-12-03_22_11_10-localhost_127.0.0.1_db_auto_phpmyadmin_4.8.3.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_auto'

    )

    ;

  3. if

    (

    !

    $conn

    )

    {
  4. die

    (

    "Error: Cam't 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. <

    head>
  5. <

    meta charset=

    "UTF-8"

    name=

    "viewport"

    content=

    "width=device-width, initial-scale=1"

    />
  6. <

    link

    rel=

    "stylesheet"

    type=

    "text/css"

    href=

    "css/bootstrap.css"

    />
  7. </

    head>
  8. </

    head>
  9. <

    body>
  10. <

    nav class

    =

    "navbar navbar-default"

    >
  11. <

    div class

    =

    "container-fluid"

    >
  12. <

    a class

    =

    "navbar-brand"

    href=

    "https://sourcecodester.com"

    >

    Sourcecodester</

    a>
  13. </

    div>
  14. </

    nav>
  15. <

    div class

    =

    "col-md-3"

    ></

    div>
  16. <

    div class

    =

    "col-md-6 well"

    >
  17. <

    h3 class

    =

    "text-primary"

    >

    PHP -

    Auto Search Using jQuery</

    h3>
  18. <

    hr style=

    "border-top:1px dotted #ccc;"

    />
  19. <

    div class

    =

    "col-md-4"

    >
  20. <

    form action=

    "save.php"

    method=

    "POST"

    >
  21. <

    div class

    =

    "form-group"

    >
  22. <

    label>

    Firstname</

    label>
  23. <

    input type=

    "text"

    class

    =

    "form-control"

    name=

    "firstname"

    required=

    "required"

    />
  24. </

    div>
  25. <

    div class

    =

    "form-group"

    >
  26. <

    label>

    Lastname</

    label>
  27. <

    input type=

    "text"

    class

    =

    "form-control"

    name=

    "lastname"

    required=

    "required"

    />
  28. </

    div>
  29. <

    div class

    =

    "form-group"

    >
  30. <

    label>

    Address</

    label>
  31. <

    input type=

    "text"

    class

    =

    "form-control"

    name=

    "address"

    required=

    "required"

    />
  32. </

    div>
  33. <

    center><

    button name=

    "save"

    class

    =

    "btn btn-primary"

    ><

    span class

    =

    "glyphicon glyphicon-save"

    ></

    span>

    Add Member</

    button></

    center>
  34. </

    form>

  35. </

    div>
  36. <

    div class

    =

    "col-md-8"

    >
  37. <

    div class

    =

    "form-group"

    >
  38. <

    input class

    =

    "form-control"

    type=

    "text"

    id=

    "search"

    placeholder=

    "Search here..."

    />
  39. </

    div>
  40. <

    div id=

    "result"

    ></

    div>
  41. </

    div>
  42. </

    div>
  43. </

    body>
  44. <

    script src=

    "js/jquery-3.2.1.min.js"

    >

    </script>
  45. <

    script src=

    "js/bootstrap.js"

    >

    </script>
  46. <

    script src=

    "js/script.js"

    >

    </script>
  47. </

    html>

Creating the PHP Query

This code contains the php query of the application. This code will store the data inputs to the database server. To do this just copy and write these block of codes below inside the text editor, then save it as save.php.
  1. <?php
  2. require_once

    'conn.php'

    ;

  3. if

    (

    ISSET

    (

    $_POST

    [

    'save'

    ]

    )

    )

    {

  4. $firstname

    =

    $_POST

    [

    'firstname'

    ]

    ;
  5. $lastname

    =

    $_POST

    [

    'lastname'

    ]

    ;
  6. $address

    =

    $_POST

    [

    'address'

    ]

    ;

  7. $conn

    ->

    query

    (

    "INSERT INTO `member` VALUES('', '$firstname

    ', '$lastname

    ', '$address

    ')"

    )

    or die

    (

    mysqli_errno

    (

    )

    )

    ;

  8. header

    (

    'location: index.php'

    )

    ;

  9. }
  10. ?>

Creating the Main Function

This code contains the main function of the application. This code will auto search a specific data in the database when the keyword is entered. To make this just copy and write these block of codes below inside the text editor, then save it as shown below.
auto_search.php
  1. <?php
  2. require_once

    'conn.php'

    ;

  3. if

    (

    ISSET

    (

    $_POST

    [

    'search'

    ]

    )

    )

    {
  4. $query

    =

    $conn

    ->

    query

    (

    "SELECT * FROM `member` WHERE (`firstname` LIKE '%"

    .

    $_POST

    [

    'search'

    ]

    .

    "%') OR (`lastname` LIKE '%"

    .

    $_POST

    [

    'search'

    ]

    .

    "%') OR (`address` LIKE '%"

    .

    $_POST

    [

    'search'

    ]

    .

    "%') "

    )

    ;

  5. $row

    =

    $query

    ->

    num_rows

    ;

  6. if

    (

    $row

    >

    0

    )

    {
  7. $output

    =

    ""

    ;
  8. $output

    .=

    "
  9. <center><h3>Search Result</h3></center>
  10. <table class='table'>
  11. <thead>
  12. <tr>
  13. <th>Firstname</th>
  14. <th>Lastname</th>
  15. <th>Address</th>
  16. </tr>
  17. </thead>
  18. <tbody>
  19. "

    ;

  20. while

    (

    $fetch

    =

    $query

    ->

    fetch_array

    (

    )

    )

    {
  21. $output

    .=

    "
  22. <tr>
  23. <td>"

    .

    $fetch

    [

    'firstname'

    ]

    .

    "</td>
  24. <td>"

    .

    $fetch

    [

    'lastname'

    ]

    .

    "</td>
  25. <td>"

    .

    $fetch

    [

    'address'

    ]

    .

    "</td>
  26. </tr>
  27. "

    ;
  28. }

  29. $output

    .=

    "
  30. </tbody>
  31. </table>
  32. "

    ;

  33. echo

    $output

    ;
  34. }

    else

    {
  35. echo

    "<center><h4>Search Not Found!</h4></center>"

    ;
  36. }

  37. }
  38. ?>

script.js
Note: To make the script works make sure you save this file inside the js directory.
  1. $(

    document)

    .ready

    (

    function

    (

    )

    {
  2. $(

    '#search'

    )

    .on

    (

    'keyup'

    ,

    function

    (

    )

    {
  3. if

    (

    $(

    "#search"

    )

    .val

    (

    )

    ==

    ""

    )

    {
  4. $(

    '#result'

    )

    .empty

    (

    )

    ;
  5. }

    else

    {
  6. var

    search =

    $(

    "#search"

    )

    .val

    (

    )

    ;
  7. $.ajax

    (

    {
  8. url:

    'auto_search.php'

    ,
  9. type:

    'POST'

    ,
  10. data:

    {

    search:

    search}

    ,
  11. dataType:

    'text'

    ,
  12. success:

    function

    (

    data)

    {
  13. $(

    "#result"

    )

    .html

    (

    data)

    ;
  14. }
  15. }

    )

    ;
  16. }
  17. }

    )

    ;
  18. }

    )

    ;

There you have it we successfully created Auto Search 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

352,162

352,176

Top