Find a user


Okay this is a tutorial showing you how to create your own ajax search script. Okay first lets create the search page.

I will be calling this page search.php

seach.php
PHP Code
  1. <?
  2. require_once "config.php"; //REQUIRE ONCE FOR SECURITY PURPOSES
  3.  
  4. $userid= $_SESSION['id'];
  5. $getuser= mysql_query("SELECT * FROM `members` WHERE `id`='$userid'");
  6. //GET USER INFO FROM DB
  7.  
  8. //NOW FOR REST OF SEARCH PAGE
  9. ?>
  10. <html><head><title>Your title</title>
  11. <script src="forms-search.js"></script>
  12. <? //BECAME LAZY FOR ALL THOSE PPL WHO WANT TO INTEGRATE THIS SCRIPT INTO THEIR SITE :) ?>
  13. </head>
  14. <body>
  15. <div align=center>
  16. <input type=text style="background-color:#FFFFFF; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:10px;" name=search-username onkeypress="search(this.value);" /><hr />
  17. <strong>
  18. <div id=txtHint>&nbsp;</div>
  19. <? //IM USING A ID IM USED TO. UNLESS YOU WANT TO CHANGE SOME STUFF IN THE 'forms-search.php' SCRIPT THEN I RECOMMEND LEAVING THIS ALONE. ?>
  20. </strong>
  21. </div>
  22. </body>
  23. </html>

Yeh smaller than my other tutorial. This code will be easier to interegrate into your site.

Okay now we need to code the javascript ajax page called forms-search.js. Heres the code for it...

forms-search.js
PHP Code
  1. var xmlHttp
  2.  
  3. function search(str)
  4. {
  5. xmlHttp=GetXmlHttpObject()
  6. if (xmlHttp==null)
  7. {
  8. alert ("Browser does not support HTTP Request")
  9. return
  10. }
  11. var url="search2.php"
  12. url=url+"?q="+str
  13. url=url+"&sid="+Math.random()
  14. xmlHttp.onreadystatechange=stateChanged
  15. xmlHttp.open("GET",url,true)
  16. xmlHttp.send(null)
  17. }
  18.  
  19. function stateChanged()
  20. {
  21. if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
  22. {
  23. document.getElementById("txtHint").innerHTML=xmlHttp.responseText
  24. }
  25. }
  26.  
  27. function GetXmlHttpObject()
  28. {
  29. var xmlHttp=null;
  30. try
  31. {
  32. // Firefox, Opera 8.0+, Safari
  33. xmlHttp=new XMLHttpRequest();
  34. }
  35. catch (e)
  36. {
  37. //Internet Explorer
  38. try
  39. {
  40. xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  41. }
  42. catch (e)
  43. {
  44. xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  45. }
  46. }
  47. return xmlHttp;
  48. }

Wow. I dont expect u to remember the above code. In fact i just copied and pasted it from a page in my server. Okay now that this page is done we now need to code one more page. We will call this page search2.php Heres the coding for it...

search2.php
PHP Code
  1. <?php
  2. $q= $_GET["q"];
  3. require_once "config.php";
  4. $sql="SELECT * FROM `members` WHERE username LIKE '%$q%'";
  5.  
  6. $result = mysql_query($sql);
  7. $check= mysql_num_rows($result);
  8.  
  9. if ($check == "0" || !isset($q)){
  10. echo "NO SUCH USER WAS FOUND!";
  11. }else{
  12.  
  13. echo "<table border='1' bordercolor=black frame=box rules=all cellpadding=0 cellspacing=0 align=center bgcolor=#333333 width=100%>
  14. <tr bgcolor=red>
  15. <th><div align=center>USERNAME</strong></div></th>
  16. </tr>";
  17.  
  18. while($row = mysql_fetch_array($result)){
  19. echo "<tr>";
  20. $usersname= $row['username'];
  21. echo "<td><div align=center>$usersname</div></td>";
  22. echo "</tr>";
  23. }
  24. echo "</table>";
  25. }
  26. ?>

Okay now your done. Save all your pages and if youve followed (or copied and pasted ;) ) correctly then you shud have a nice ajax search script.
ilyas-shezad's Avatar
Views:
3,546
Rating:
Posted on Sunday 4th May 2008 at 02:40 AM
JakkyD
JakkyD's Avatar
Nice work.

Just a little extra in search2.php:

Find- <div align=center>$usersname</div>

Replace with- <div align=center><a href='members.php?user=$usersname'>$usersname</a></div>

This adds a link to the person(s) you searched for's profile.
Posted on Saturday 19th April 2008 at 12:54 PM
cyruswu
cyruswu's Avatar
Fix it up yourself.

and use include instead of require.
Posted on Friday 4th April 2008 at 10:25 PM
UrbanTwitch
UrbanTwitch's Avatar
Doesn't work:

Warning: require_once(config.php) [function.require-once]: failed to open stream: No such file or directory in /home/jsfdan/public_html/search/search.php on line 2

Fatal error: require_once() [function.require]: Failed opening required 'config.php' (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/jsfdan/public_html/search/search.php on line 2
Posted on Monday 15th October 2007 at 08:54 PM
Arudis
Arudis's Avatar
wow man works goood thx