<!doctype html>
<html>
<head>
<title>AJAX Sample search page</title>
</head>
<body>
<h2>Super Hero Contact List</h2>
Search <input type="text" id="search" /> <input type="button" value="Search" id="search_button" /><br/>
<table id="superheros">
<thead>
<tr>
<th>Firstname</th><th>Surname</th><th>Age</th><th>Email</th>
</tr>
</thead>
<?php require_once('data.php'); ?>
</table>
</body>
</html>
<?php
/*
FIRST: DEFINE OUR TEST DATA.
FOR OTHER IMPLEMENTATIONS THIS ARRAY COULD BE
POPULATED FROM A DATABASE OR ANY OTHER SOURCE
*/
$values = array(
array('firstname' => 'Peter', 'surname' => 'Parker', 'age' =>'21', 'email' =>'peter@arachnaphopia.com'),
array('firstname' => 'Clark', 'surname' => 'Kent', 'age' =>'36', 'email' =>'kal-el@krypton.com'),
array('firstname' => 'Bruce', 'surname' => 'Banner', 'age' =>'28', 'email' =>'hulk@bigandgreen.com'),
array('firstname' => 'Steven', 'surname' => 'RogersS', 'age' =>'95', 'email' =>'captain@merica.com'),
array('firstname' => 'Bruce', 'surname' => 'Wayne', 'age' =>'32', 'email' =>'bruce@batcave.com')
);
/* LOOP THROUGH THE ARRAY AND CONSTRUCT OUR ROWS */
foreach($values as $v) :
echo '<tr>
<td>' . $v['firstname'] . '</td><td>' . $v['surname'] . '</td><td>' . $v['age'] . '</td><td>' . $v['email'] .'</td>
</tr>';
endforeach;
?>
That's about it - the above code will generate the results for our table.
$searchstr = implode(' ', $row);
With the above string we can now do a regular expression match on our search expression.
preg_match("/$pattern/", $searchstr);
If preg_match returns a value > 0 we have a match.
$pattern = empty($_POST['search']) ? '' : $_POST['search'];
<?php
$values = array(
array('firstname' => 'Peter', 'surname' => 'Parker', 'age' =>'21', 'email' =>'peter@arachnaphopia.com'),
array('firstname' => 'Clark', 'surname' => 'Kent', 'age' =>'36', 'email' =>'kal-el@krypton.com'),
array('firstname' => 'Bruce', 'surname' => 'Banner', 'age' =>'28', 'email' =>'hulk@bigandgreen.com'),
array('firstname' => 'Steven', 'surname' => 'RogersS', 'age' =>'95', 'email' =>'captain@merica.com'),
array('firstname' => 'Bruce', 'surname' => 'Wayne', 'age' =>'32', 'email' =>'bruce@batcave.com')
);
/*
RETRIEVE THE SEARCH EXPRESSION
FROM THE POST. DEFAULT TO EMPTY
STRING IF NOT PRESENT
*/
$expr = empty($_POST['search']) ? '' : $_POST['search'];
foreach($values as $v) {
/* CREATE OUR SEARCH STRING */
$searchstr = implode(" ", $v);
/* LOOK FOR A PATTERN MATCH */
$rv = preg_match("/$expr/", $searchstr);
/* IF MATCHED OUTPUT ROW */
if ($rv > 0) :
echo '<tr>
<td>' . $v['firstname'] . '</td><td>' . $v['surname'] . '</td><td>' . $v['age'] . '</td><td>' . $v['email'] .'</td>
</tr>';
endif;
}
?>
That's all we need to do on the data.php side. If no POST parameter is supplied all results will be returned as the preg_match will work against an empty string. In theory we could make the code a bit more efficient by putting a test for an empty expression around the preg_match - this is left to the reader to implement if required.
/* FIRST UP INCLUDE JQUERY */
<script src="http://code.jquery.com/jquery.js"></script>
<script type="text/javascript">
$(function() {
/*
CREATE AN EVENT HANDLER FOR
THE SEARCH BUTTON
/*
$('#search_button').click(function(e) {
/* DISABLE DEFAULT BEHAVIOUR FOR THE BUTTON */
e.preventDefault();
/* POST THE SEARCH TERM TO OUR data.php SCRIPT */
$.post('data.php', {search: $('#search').val()}, function(html) {
/*
LOAD THE RETURNED HTML TO THE
BODY OF THE RESULTS TABLE
*/
$('#superheros tbody').html(html);
});
});
});
</script>
And that should be that. Here is a full listing of the final container file
<!doctype html>
<html>
<head>
<title>Test</title>
<script src="http://code.jquery.com/jquery.js"></script>
<script type="text/javascript">
$(function() {
$('#search_button').click(function(e) {
e.preventDefault();
$.post('data.php', {search: $('#search').val()}, function(html) {
$('#superheros tbody').html(html);
});
});
});
</script>
<style type="text/css">
</style>
</head>
<body>
<h2>Super Hero Contact List</h2>
Search <input type="text" id="search" /> <input type="button" value="Search" id="search_button" /><br/>
<table id="superheros">
<thead>
<tr>
<th>Firstname</th><th>Surname</th><th>Age</th><th>Email</th>
</tr>
</thead>
<?php require_once('data.php'); ?>
</table>
</body>
</html>
$rv = preg_match("/$expr/i", $searchstr);
And there it is. A very simple but fairly versatile AJAX search function with a common script to render the results for both AJAX and non-AJAX requests.
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (10)
Commented:
Commented:
How could we create a simple search form where lets say for example I have a page on my website called 'About Us' and a heading tag on that page called 'About Us' and I'm on the index page and I type 'about' in the search bar and hit enter it find the matching word and redirects to that page where the matching word is located?
Author
Commented:What you are describing here is for an indexed site search - you might want to look at tools like https://www.elastic.co/
Commented:
Author
Commented:View More