Tuesday, June 5, 2012

Simple search php mysql

Simple search php mysql


When a database is populated with data when you have large number of data it is not easy to find the data exactly you want.because of that we need  to search the data we need.For this purpose I have created HTML File with search input which is displaying  bellow.

Select from Person name

With this search application I have included a loader.gif when search button pressed loader image  start to appear and when search is completed it disappears . It shows user the search is in progress you can copy it from bellow and should be stored in your web root.
The codes for above HTML file is bellow.


<html>
<head>
    <title>Search</title>
    </head>
<body>

    <div id="formContent" align="center">
        <form action="search.php" method="post" enctype="multipart/form-data" onsubmit="return show_loading();">

        <fieldset style="width: 100px; height: 100px;">
            <legend>Select from Person name</legend>

            <table border="0" width=10>
                <tr>
                    <td><label for="search">Person Name: </label></td>
                    <td><input type="text" id="search" name="term" value="" onkeyup="ajax_showOptions(this,'getCountriesByLetters',event)">
                    <input type="submit" id="submit" name="submit" value="Search"><span id="pub_loading"><img src="loader.gif" width="100" height="100" alt="Loading" /></span></td>
                </tr>  
              </fieldset>  
        </table>
        </form>
    </div>
<style type="text/css">
<!--
#pub_loading {display:none;}
-->
</style>
<script type="text/javascript">
function show_loading(){
var loadingAnim = document.getElementById('pub_loading');
loadingAnim.innerHTML = '<img src="loader.gif" width="100" height="100" alt="Loading" />';
loadingAnim.style.display = 'inline';
}
</script>
</div>
</table>
</body>
</html>

search.php which search the last name we submitted from "my_database,person table". Here in the title of this file, I have included the codes to display the day of the week and the date. Completed codes are as displaying bellow.

<html>
<head>
<title><?php print("("); print date("l"); print(", "); print date("d/m/y"); print(")");?></title>
</head>
<?php
$host="localhost";
$username="";
$password="";
$db_name="my_database";
$tbl_name="person";
$term=$_POST['term'];
mysql_connect("$host","root","$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name WHERE LastName LIKE '$term%'";
$result=mysql_query($sql);

?>
<table align="center" bgcolor="#b3dfef">

<tr>
<td align="center" bgcolor="#FFFFFF"><strong>ID</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>First Name</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Last Name</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Birth Day</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>

<tr>
<td bgcolor="#FFFFFF"><?php echo $rows['id']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['FirstName']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['LastName']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['birth_day']; ?></td>

<?php
}
mysql_close();
?>
</table>

No comments:

Post a Comment