Skip to main 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:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>PHP, jQuery search demo</title>
<link rel="stylesheet" type="text/css" href="my.css">



</head>
<body>
<div id="container">
<div style="margin:20px auto; text-align: center;">
<form method="post" action="do_search.php">
<input type="text" name="search" id="search_box" class='search_box'/>
<input type="submit" value="Search" class="search_button" /><br />
</form>
</div>
<div>

<div id="searchresults">Search results for <span class="word"></span></div>
<ul id="results" class="update">
</ul>

</div>
</div>

</body>

</html>

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:

<?php
//if we got something through $_POST
if (isset($_POST['search'])) {
// here you would normally include some database connection
include('db.php');
$db = new db();
// never trust what user wrote! We must ALWAYS sanitize user input
$word = mysql_real_escape_string($_POST['search']);
// build your search query to the database
$sql = "SELECT title, url FROM pages WHERE content LIKE '%" . $word . "%' ORDER BY title LIMIT 10";
// get results
$row = $db->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 = '<span class="found">' . $word . '</span>';
$end_result .= '<li>' . str_ireplace($word, $bold, $result) . '</li>';
}
echo $end_result;
} else {
echo '<li>No results found</li>';
}
}
?>

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:

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; }

 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.

Ref: http://www.codeforest.net/simple-search-with-php-jquery-and-mysql