how can create simple search fetches data database , displays in table while typing, using php5 , jquery. i've seen many tutorials of them using php4 (mysql_connect..)?
ps: can in single php file?
when user types character in input field above, function "showresult()" executed. function triggered "onkeyup" event:
<html> <head> <script> function showresult(str) { if (str.length==0) { document.getelementbyid("livesearch").innerhtml=""; document.getelementbyid("livesearch").style.border="0px"; return; } if (window.xmlhttprequest) { // code ie7+, firefox, chrome, opera, safari xmlhttp=new xmlhttprequest(); } else { // code ie6, ie5 xmlhttp=new activexobject("microsoft.xmlhttp"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readystate==4 && xmlhttp.status==200) { document.getelementbyid("livesearch").innerhtml=xmlhttp.responsetext; document.getelementbyid("livesearch").style.border="1px solid #a5acb2"; } } xmlhttp.open("get","livesearch.php?q="+str,true); xmlhttp.send(); } </script> </head> <body> <form> <input type="text" size="30" onkeyup="showresult(this.value)"> <div id="livesearch"></div> </form> </body> </html>
livesearch.php : file process input place in same place
<?php //get q parameter url $term=$_get["q"]; $con = mysql_connect ("localhost", "root", ""); mysql_select_db ("music", $con); if (!$con) { die ("could not connect: " . mysql_error()); } $sql = mysql_query("select * entries title '%$term%'") or die (mysql_error()); while ($row = mysql_fetch_array($sql, mysql_assoc)){ echo 'title: ' .$row['title']; echo '<br /> artist: ' .$row['artist']; echo '<br /> album: '.$row['album']; echo '<br /> location: '.$row['location']; echo '<br /> media: '.$row['media']; } mysql_close($con); ?>
Comments
Post a Comment