Jun
6

Ajax Tutorial: How to Create Ajax Search Using PHP jQuery and MYSQL

Author ZeeShaN    Category AJAX, JQuery, PHP, Web Development     Tags


This tutorial shows how to create simple and attractive Ajax based search using PHP, jQuery, MySQL and Ajax.

Table Structure:

1
2
3
4
5
6
CREATE TABLE `search` (
  `id` int(11) NOT NULL auto_increment,
  `text` varchar(255) NOT NULL,
  `link` varchar(255) NOT NULL,
  PRIMARY KEY  (`id`)
)

Javascript Code:

1
2
3
4
5
6
7
8
$(".searchBtn").click(function(){
	//show the loading bar
	showLoader();
	$('#sub_cont').fadeIn(1500);
 
	$("#content #sub_cont").load("search.php?val=" + $("#search").val(), hideLoader());
 
});

HTML

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<div class="textBox">
        <input type="text" value="" maxlength="100" name="searchBox" id="search">
	<div class="searchBtn">
	&nbsp;
       </div>
</div>
<br clear="all" />
<div id="content">
	<div class="search-background">
	<label><img src="loader.gif" alt="" /></label>
	</div>
	<div id="sub_cont">	
	</div>
</div>

PHP Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
include("dbcon.php");
$rec = $_REQUEST['val'];
 
//get table contents
if($_REQUEST['val'])
{
	$sql = "select * from search where text like '%$rec%'";
}
else
{
	$sql = "select * from search ";
}
 
$rsd = mysql_query($sql);
$total =  mysql_num_rows($rsd);

Add To Facebook Stumble This Digg This Add To Del.icio.us Add To Reddit Add To Yahoo Add To Twitter


Written by ZeeShaN

ZeeShaN RasooL is a web developer who loves to work in latest technologies to create more interactive dynamic and beautiful web pages.







Enter your Email:

Click Here for Popular

Who I Am

Zeeshan Rasool

Software Engineer - PHP
Lahore - Pakistan

zeeshan(@)99points.info
Skype: zeeshan-rasool
gTalk: zishan.rasool85

Categories

Tags

Comments