Hi friend, I hope all are good now I am describing how to create auto complete suggestion with help of jquery and php.
Step1:
first we will create HTML layout file which is contain all reference file as well as all CSS style code.
HTML CODE
- <link rel=“stylesheet” type=“text/css” href=“style.css” />
- <script type=“text/javascript” src=“jquery-1.2.1.pack.js”></script>
- <script type=“text/javascript” src=“js/jconfirmaction.jquery.js”></script>
- <script type=“text/javascript”>
- $(document).ready(function() {
- $(‘.ask’).jConfirmAction();
- });
- </script>
- <script type=“text/javascript” src=“js/jquery.min.js”></script>
- <script language=“javascript” type=“text/javascript” src=“js/niceforms.js”></script>
- <link rel=“stylesheet” type=“text/css” media=“all” href=“js/niceforms-default.css” />
- <script type=“text/javascript”>
- function lookup(inputString) {
- if(inputString.length == 0) {
- // Hide the suggestion box.
- $(‘#suggestions’).hide();
- } else {
- $.post(“rpc.php”, {queryString: “”+inputString+“”}, function(data){
- if(data.length >0) {
- $(‘#suggestions’).show();
- $(‘#autoSuggestionsList’).html(data);
- }
- });
- }
- } // lookup
- function fill(thisValue) {
- $(‘#inputString’).val(thisValue);
- setTimeout(“$(‘#suggestions’).hide();”, 200);
- }
- </script>
- <style type=“text/css”>
- body {
- font-family: Helvetica;
- font-size: 11px;
- color: #000;
- }
- h3 {
- margin: 0px;
- padding: 0px;
- }
- .suggestionsBox {
- position: absolute;
- left: 30px;
- margin: 10px 0px 0px 0px;
- width: 200px;
- background-color: #212427;
- -moz-border-radius: 7px;
- -webkit-border-radius: 7px;
- border: 2px solid #000;
- color: #fff;
- }
- .suggestionList {
- margin: 0px;
- padding: 0px;
- }
- .suggestionList li {
- margin: 0px 0px 3px 0px;
- padding: 3px;
- cursor: pointer;
- }
- .suggestionList li:hover {
- background-color: #659CD8;
- }
- </style>
- </head>
- <body>
- <div>
- <div>
- Type your county:
- <input type=“text” size=“30″ value=“” id=“inputString” onkeyup=“lookup(this.value);” onblur=“fill();” />
- </div>
- <div class=“suggestionsBox” id=“suggestions” style=“display: none;”>
- <img src=“upArrow.png” style=“position: relative; top: -12px; left: 30px;z-index:8880″ alt=“upArrow” />
- <div class=“suggestionList” id=“autoSuggestionsList”>
-
- </div>
- </div>
- <div>dddddddddddddddddddd<div>
- <div class=“form”>
- <form action=“” method=“post” class=“niceform”>
- <fieldset>
- <dl>
- <dt><label for=“email”>Email Address:</label></dt>
- <dd><input type=“text” name=“” id=“” size=“54″ /></dd>
- </dl>
- <dl>
- <dt><label for=“password”>Password:</label></dt>
- <dd><input type=“text” name=“” id=“” size=“54″ /></dd>
- </dl>
- <dl>
- <dt><label for=“gender”>Select categories:</label></dt>
- <dd>
- <select size=“1″ name=“gender” id=“”>
- <option value=“”>Select option 1</option>
- <option value=“”>Select option 2</option>
- <option value=“”>Select option 3</option>
- <option value=“”>Select option 4</option>
- <option value=“”>Select option 5</option>
- </select>
- </dd>
- </dl>
- <dl>
- <dt><label for=“interests”>Select tags:</label></dt>
- <dd>
- <input type=“checkbox” name=“interests[]“ id=“” value=“” /><label class=“check_label”>Web design</label>
- <input type=“checkbox” name=“interests[]“ id=“” value=“” /><label class=“check_label”>Business</label>
- <input type=“checkbox” name=“interests[]“ id=“” value=“” /><label class=“check_label”>Simple</label>
- <input type=“checkbox” name=“interests[]“ id=“” value=“” /><label class=“check_label”>Clean</label>
- </dd>
- </dl>
- <dl>
- <dt><label for=“color”>Select type</label></dt>
- <dd>
- <input type=“radio” name=“type” id=“” value=“” /><label class=“check_label”>Basic</label>
- <input type=“radio” name=“type” id=“” value=“” /><label class=“check_label”>Medium</label>
- <input type=“radio” name=“type” id=“” value=“” /><label class=“check_label”>Premium</label>
- </dd>
- </dl>
- <dl>
- <dt><label for=“upload”>Upload a File:</label></dt>
- <dd><input type=“file” name=“upload” id=“upload” /></dd>
- </dl>
- <dl>
- <dt><label for=“comments”>Message:</label></dt>
- <dd><textarea name=“comments” id=“comments” rows=“5″ cols=“36″></textarea></dd>
- </dl>
- <dl>
- <dt><label></label></dt>
- <dd>
- <input type=“checkbox” name=“interests[]“ id=“” value=“” /><label class=“check_label”>I agree to the <a href=“#”>terms & conditions</a></label>
- </dd>
- </dl>
- <dl class=“submit”>
- <input type=“submit” name=“submit” id=“submit” value=“Submit” />
- </dl>
- </fieldset>
- </form>
- </div>
- </div>
$(document).ready(function() {
$(‘.ask’).jConfirmAction();
});
</script>
<script type=”text/javascript” src=”js/jquery.min.js”></script>
<script language=”javascript” type=”text/javascript” src=”js/niceforms.js”></script>
<link rel=”stylesheet” type=”text/css” media=”all” href=”js/niceforms-default.css” />
<script type=”text/javascript”>
function lookup(inputString) {
if(inputString.length == 0) {
// Hide the suggestion box.
$(‘#suggestions’).hide();
} else {
$.post(“rpc.php”, {queryString: “”+inputString+”"}, function(data){
if(data.length >0) {
$(‘#suggestions’).show();
$(‘#autoSuggestionsList’).html(data);
}
});
}
} // lookup
function fill(thisValue) {
$(‘#inputString’).val(thisValue);
setTimeout(“$(‘#suggestions’).hide();”, 200);
}
</script>
<style type=”text/css”>
body {
font-family: Helvetica;
font-size: 11px;
color: #000;
}
h3 {
margin: 0px;
padding: 0px;
}
.suggestionsBox {
position: absolute;
left: 30px;
margin: 10px 0px 0px 0px;
width: 200px;
background-color: #212427;
-moz-border-radius: 7px;
-webkit-border-radius: 7px;
border: 2px solid #000;
color: #fff;
}
.suggestionList {
margin: 0px;
padding: 0px;
}
.suggestionList li {
margin: 0px 0px 3px 0px;
padding: 3px;
cursor: pointer;
}
.suggestionList li:hover {
background-color: #659CD8;
}
</style>
</head>
<body>
<div>
<div>
Type your county:
<br />
<input type=”text” size=”30″ value=”" id=”inputString” onkeyup=”lookup(this.value);” onblur=”fill();” />
</div>
<div class=”suggestionsBox” id=”suggestions” style=”display: none;”>
<img src=”upArrow.png” style=”position: relative; top: -12px; left: 30px;z-index:8880″ alt=”upArrow” />
<div class=”suggestionList” id=”autoSuggestionsList”>
</div>
</div>
<div>dddddddddddddddddddd<div>
<div class=”form”>
<form action=”" method=”post” class=”niceform”>
<fieldset>
<dl>
<dt><label for=”email”>Email Address:</label></dt>
<dd><input type=”text” name=”" id=”" size=”54″ /></dd>
</dl>
<dl>
<dt><label for=”password”>Password:</label></dt>
<dd><input type=”text” name=”" id=”" size=”54″ /></dd>
</dl>
<dl>
<dt><label for=”gender”>Select categories:</label></dt>
<dd>
<select size=”1″ name=”gender” id=”">
<option value=”">Select option 1</option>
<option value=”">Select option 2</option>
<option value=”">Select option 3</option>
<option value=”">Select option 4</option>
<option value=”">Select option 5</option>
</select>
</dd>
</dl>
<dl>
<dt><label for=”interests”>Select tags:</label></dt>
<dd>
<input type=”checkbox” name=”interests[]” id=”" value=”" /><label class=”check_label”>Web design</label>
<input type=”checkbox” name=”interests[]” id=”" value=”" /><label class=”check_label”>Business</label>
<input type=”checkbox” name=”interests[]” id=”" value=”" /><label class=”check_label”>Simple</label>
<input type=”checkbox” name=”interests[]” id=”" value=”" /><label class=”check_label”>Clean</label>
</dd>
</dl>
<dl>
<dt><label for=”color”>Select type</label></dt>
<dd>
<input type=”radio” name=”type” id=”" value=”" /><label class=”check_label”>Basic</label>
<input type=”radio” name=”type” id=”" value=”" /><label class=”check_label”>Medium</label>
<input type=”radio” name=”type” id=”" value=”" /><label class=”check_label”>Premium</label>
</dd>
</dl>
<dl>
<dt><label for=”upload”>Upload a File:</label></dt>
<dd><input type=”file” name=”upload” id=”upload” /></dd>
</dl>
<dl>
<dt><label for=”comments”>Message:</label></dt>
<dd><textarea name=”comments” id=”comments” rows=”5″ cols=”36″></textarea></dd>
</dl>
<dl>
<dt><label></label></dt>
<dd>
<input type=”checkbox” name=”interests[]” id=”" value=”" /><label class=”check_label”>I agree to the <a href=”#”>terms & conditions</a></label>
</dd>
</dl>
<dl class=”submit”>
<input type=”submit” name=”submit” id=”submit” value=”Submit” />
</dl>
</fieldset>
</form>
</div>
</div>
Step2:
Now we will create PHP script file, It has all Sql Query to get matching records for show auto suggestion.
PHP Code
- <?php
- // PHP5 Implementation - uses MySQLi.
- // mysqli(‘localhost’, ’yourUsername’, ’yourPassword’, ’yourDatabase’);
- $dbLink = mysql_connect(‘localhost’, ‘root’ ,”) ;
- $db = mysql_select_db(‘test’,$dbLink);
- //$db = new mysql_connect(‘localhost’, ’root’ ,”, ’test’);
- if(!$db) {
- // Show error if we cannot connect.
- echo ‘ERROR: Could not connect to the database.’;
- } else {
- // Is there a posted query string?
- if(isset($_POST['queryString'])) {
- $queryString = mysql_real_escape_string($_POST['queryString']);
- // Is the string length greater than 0?
- if(strlen($queryString) >0) {
- // Run the query: We use LIKE ’$queryString%’
- // The percentage sign is a wild-card, in my example of countries it works like this…
- // $queryString = ’Uni’;
- // Returned data = ’United States, United Kindom’;
- // YOU NEED TO ALTER THE QUERY TO MATCH YOUR DATABASE.
- // eg: SELECT yourColumnName FROM yourTable WHERE yourColumnName LIKE ’$queryString%’ LIMIT 10
- $query = mysql_query(“SELECT value FROM countries WHERE value LIKE ’$queryString%’ LIMIT 10″);
- if($query) {
- // While there are results loop through them - fetching an Object (i like PHP5 btw!).
- while ($result = mysql_fetch_row($query)) {
- // Format the results, im using <li> for the list, you can change it.
- // The onClick function fills the textbox with the result.
- // YOU MUST CHANGE: $result->value to $result->your_colum
- echo ‘<li onClick=”fill(\”.$result[0].‘\’);”>’.$result[0].‘</li>’;
- }
- } else {
- echo ‘ERROR: There was a problem with the query.’;
- }
- } else {
- // Dont do anything.
- } // There is a queryString.
- } else {
- echo ‘There should be no direct access to this script!’;
- }
- }
- ?>
// PHP5 Implementation – uses MySQLi.
// mysqli(‘localhost’, ‘yourUsername’, ‘yourPassword’, ‘yourDatabase’);
$dbLink = mysql_connect(‘localhost’, ‘root’ ,”) ;
$db = mysql_select_db(‘test’,$dbLink);
//$db = new mysql_connect(‘localhost’, ‘root’ ,”, ‘test’);
if(!$db) {
// Show error if we cannot connect.
echo ‘ERROR: Could not connect to the database.’;
} else {
// Is there a posted query string?
if(isset($_POST['queryString'])) {
$queryString = mysql_real_escape_string($_POST['queryString']);
// Is the string length greater than 0?
if(strlen($queryString) >0) {
// Run the query: We use LIKE ‘$queryString%’
// The percentage sign is a wild-card, in my example of countries it works like this…
// $queryString = ‘Uni’;
// Returned data = ‘United States, United Kindom’;
// YOU NEED TO ALTER THE QUERY TO MATCH YOUR DATABASE.
// eg: SELECT yourColumnName FROM yourTable WHERE yourColumnName LIKE ‘$queryString%’ LIMIT 10
$query = mysql_query(“SELECT value FROM countries WHERE value LIKE ‘$queryString%’ LIMIT 10″);
if($query) {
// While there are results loop through them – fetching an Object (i like PHP5 btw!).
while ($result = mysql_fetch_row($query)) {
// Format the results, im using <li> for the list, you can change it.
// The onClick function fills the textbox with the result.
// YOU MUST CHANGE: $result->value to $result->your_colum
echo ‘<li onClick=”fill(\”.$result[0].’\');”>’.$result[0].’</li>’;
}
} else {
echo ‘ERROR: There was a problem with the query.’;
}
} else {
// Dont do anything.
} // There is a queryString.
} else {
echo ‘There should be no direct access to this script!’;
}
}
?>