0

Hello guys i learn something new so i thought to share it with you people.This tutorial is about when you search some data in a search bar and as you enter any key word e.g 'a' so it start looking for keywords starting with 'a' and highlight it.In this way entering full name e.g ahmed it highlight the name ahmed.i used jQuery for this purpose and little bit of styling for the highlight purpose.i have used inline scripting for the purpose of ease for you people but prefer external scripting as it is the more proficient way.Here the code

<html>
<head>
 
<style type="text/css">
 
#table1, #table1 tr, #table1 td
{
border: 1px solid #666;
}
 
.highlight
{
background-color:#CCC;
}
 
</style>
 
<script type="text/javascript" src="jquery.js"></script> <!-- need jquery.js file-->
 
 
<script type="text/javascript">
 
$(document).ready(function(){
 
$("#search1").keyup(function(){
search2 =$(this).val();
 
 
$("#table1 tr td").removeClass('highlight');
 
if(jQuery.trim(search2) != ''){
$("#table1 tr td:contains('" + search2 +"')").addClass('highlight');
}
 
});
});
 
</script>
 
</head>
<body>
 
<?php
echo '<p>Search : <input type="text" id="search1" /></p>';
 
$connect = mysql_connect("localhost","root",""); // change here
mysql_select_db("project");
 
$limit = 14;
$last = 24;
 
$sql = mysql_query("SELECT `id`, `name` FROM `stu_info` WHERE `id` > '$limit' AND `id` < '$last'"); // change here
 
echo "<br /><table id='table1'>";
while($show = mysql_fetch_assoc($sql))
{
echo $id = '<tr id="test"><td>'.$show['id'].'<br /></tr></td>'; //change here
echo $name = '<tr><td>'.$show['name'].'</tr></td>'; //change here
}
echo "</table></p>";
 
?>
 
</body>
</html>
2
Contributors
3
Replies
4
Views
5 Years
Discussion Span
Last Post by Taywin
0

Great to hear that you learn something new. But you should also know the limitation of this code as well. Also, this code could be susceptible to XSS attack.

0

Then you should also add the limitation and any possible vulnerabilities. It is nice to give a demo, but you should not give it out without possible future damages. If you want to teach others, you need to try to teach them the right way or as close as you could. Think about it as you are teaching someone using a knife. You told the person that you just pick it up from one end and cut it through an object on a cutting board. That's all. Now if the person takes the lesson and use the knife without care, what would possibly happen? That's what I am trying to explain to you. :)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.