Skip to content

Simple search with PHP, jQuery and MySQL

You can see the demo for this tutorial here.

The best way to keep your web site visitors, is to help them find what they are looking for. If you manage to do it in a simple and beautiful way, their feeling of loyalty will increase and they will come back and search for more great articles.

I will show you how to build a simple, yet powerful, search form from which you will show your users search results without refreshing the page, giving your web site a nice touch.

I will create two files: search.php which will contain HTML and JavaScript and do_search.php which will contain the PHP side. First, our HTML and jQuery file:

[code lang=”javascript”]




PHP, jQuery search demo





Search results :



[/code]

It is a normal HTML form that will do the POST to our back end file do_search.php.
For explaining the jQuery part, please read the comments above.

And here is our do_search.php:

[code lang=”php”]
select_list($sql);
if(count($row)) {
$end_result = ”;
foreach($row as $r) {
$result = $r[‘title’];
// we will use this to bold the search word in result
$bold = ‘‘ . $word . ‘‘;
$end_result .= ‘

  • ‘ . str_ireplace($word, $bold, $result) . ‘
  • ‘;
    }
    echo $end_result;
    } else {
    echo ‘

  • No results found
  • ‘;
    }
    }
    ?>
    [/code]

    PHP code is commented so you can easily follow what is going on. If you got some results from the database, you will show them to your user and even bold the word that the user searched for inside results.

    This is some simple CSS I used to format the form and the results:

    [code lang=”css”]
    body{ font-family:Arial, Helvetica, sans-serif; }
    *{ margin:0;padding:0; }
    #container { margin: 0 auto; width: 600px; }
    a { color:#DF3D82; text-decoration:none }
    a:hover { color:#DF3D82; text-decoration:underline; }
    ul.update { list-style:none;font-size:1.1em; margin-top:10px }
    ul.update li{ height:30px; border-bottom:#dedede solid 1px; text-align:left;}
    ul.update li:first-child{ border-top:#dedede solid 1px; height:30px; text-align:left; }
    #flash { margin-top:20px; text-align:left; }
    #searchresults { text-align:left; margin-top:20px; display:none; font-family:Arial, Helvetica, sans-serif; font-size:16px; color:#000; }
    .word { font-weight:bold; color:#000000; }
    #search_box { padding:4px; border:solid 1px #666666; width:300px; height:30px; font-size:18px;-moz-border-radius: 6px;-webkit-border-radius: 6px; }
    .search_button { border:#000000 solid 1px; padding: 6px; color:#000; font-weight:bold; font-size:16px;-moz-border-radius: 6px;-webkit-border-radius: 6px; }
    .found { font-weight: bold; font-style: italic; color: #ff0000; }
    h2 { margin-right: 70px; }
    [/code]

    I showed the simplest way to build modern search form and get the results without refreshing the page. I hope you enjoyed this tutorial. You can ask questions in comments.

    You can see the demo for this tutorial here.

    Tags: