Skip to content

Instantly share code, notes, and snippets.

@pbesra
Last active March 16, 2017 21:58
Show Gist options
  • Save pbesra/f4cb7ab3a138177d4ac5d0b58f58cb0a to your computer and use it in GitHub Desktop.
Save pbesra/f4cb7ab3a138177d4ac5d0b58f58cb0a to your computer and use it in GitHub Desktop.
auto suggest application in php using ajax, html and mysql
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function findmatch(){
if(window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest();
}else{
xmlhttp=new ActiveXobject('Microsoft.XMLHTTP');
}
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState==4 && xmlhttp.status==200){
document.getElementById('results').innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open('GET', 'include.inc.php?search_text='+document.search.search_text.value, true);
xmlhttp.send();
}
</script>
</head>
<body>
<form id="search" name="search">
Type a name :<br>
<input type="text" name="search_text" onkeyup="findmatch();">
</form>
<div id="results" ></div>
</body>
</html>
<?php
if(isset($_GET['search_text'])){
$search_text=$_GET['search_text'];
}
mysql_connect('localhost', 'root', '');
mysql_select_db('a_database');
if(!empty($search_text)){
$query="SELECT firstname FROM biodata WHERE firstname LIKE '".mysql_real_escape_string($search_text)."%'";
$query_run=mysql_query($query);
if(mysql_num_rows($query_run)==0){
echo 'No results found.';
}
while($query_row=mysql_fetch_assoc($query_run)){
echo $name=$query_row['firstname'].'<br/>';
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment