PHP Ajax Search

Reply

Join Date: Apr 2007
Posts: 4
Reputation: BeerGuzler is an unknown quantity at this point 
Solved Threads: 0
BeerGuzler BeerGuzler is offline Offline
Newbie Poster

PHP Ajax Search

 
0
  #1
Apr 9th, 2007
Hi,

I'm trying to put together a PHP Ajax search against an mySQL database yet doesnt seem to work .. HELP!

I had to resort to using frameset which I dont really want to do.

what I'm trying to is perform a search against a mysql database and display the results on the same page. Want the ability to perform multiple search without a refresh or change of page.

so far this is what i have
Index.html
  1. <html>
  2. <head>
  3. <script src="selectuser.js"></script>
  4. </head>
  5. <body>
  6. <div align="center"
  7. <form name="form" action="search.php" method="get">
  8. <input type="text" name="q" />
  9. <input type="submit" name="Submit" value="Search" />
  10.  
  11. <div align="right">
  12. <select name="list">
  13. <option value="10"> 10 </option>
  14. <option value="20"> 20 </option>
  15. <option value="50"> 50 </option>
  16. </select>
  17. </div>
  18.  
  19. </form>
  20. </div>
  21. <div id="txtHint"><b>User info will be listed here.</b></div>
  22. </body>
  23. </html>

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

The code calls a search.php which performs the search against the database and creates the tables.

Search.php echos the tables using DIV .
Any ideas on how to improve this ?
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,075
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: PHP Ajax Search

 
0
  #2
Apr 11th, 2007
Originally Posted by BeerGuzler View Post
Hi,

I'm trying to put together a PHP Ajax search against an mySQL database yet doesnt seem to work .. HELP!

I had to resort to using frameset which I dont really want to do.

what I'm trying to is perform a search against a mysql database and display the results on the same page. Want the ability to perform multiple search without a refresh or change of page.

so far this is what i have
Index.html
  1. <html>
  2. <head>
  3. <script src="selectuser.js"></script>
  4. </head>
  5. <body>
  6. <div align="center"
  7. <form name="form" action="search.php" method="get">
  8. <input type="text" name="q" />
  9. <input type="submit" name="Submit" value="Search" />
  10.  
  11. <div align="right">
  12. <select name="list">
  13. <option value="10"> 10 </option>
  14. <option value="20"> 20 </option>
  15. <option value="50"> 50 </option>
  16. </select>
  17. </div>
  18.  
  19. </form>
  20. </div>
  21. <div id="txtHint"><b>User info will be listed here.</b></div>
  22. </body>
  23. </html>

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

The code calls a search.php which performs the search against the database and creates the tables.

Search.php echos the tables using DIV .
Any ideas on how to improve this ?
What error(s) are you getting? Or what isn't happening?

There are a couple of syntax errors in your JS.

Searchuser.js
  1. var xmlHttpfunction showUser(str)
  2. {

should be

  1. var xmlHttp
  2.  
  3. function showUser(str)
  4. {

Probably best to end all your JS commands with the semi-colon. This allows more than one on a line without having syntax errors. (Useful if you want to compress your JS also)

The URL you are creating will have two ? in it.

  1. var url="search.php"
  2. url=url+"?q="+str+"?list="
  3. url=url+"&sid="+Math.random()

I believe you want:

  1. var url="search.php";
  2. url=url+"?q="+str+"&list=";
  3. url=url+"&sid="+Math.random();

or simpler:

  1. var url="search.php";
  2. url += "?q="+str+"&list=";
  3. url += "&sid="+Math.random();
Last edited by digital-ether; Apr 11th, 2007 at 4:05 pm.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 1
Reputation: Cybertronics is an unknown quantity at this point 
Solved Threads: 0
Cybertronics Cybertronics is offline Offline
Newbie Poster

Re: PHP Ajax Search

 
0
  #3
Jan 17th, 2009
Originally Posted by BeerGuzler View Post
Hi,

I'm trying to put together a PHP Ajax search against an mySQL database yet doesnt seem to work .. HELP!

I had to resort to using frameset which I dont really want to do.

what I'm trying to is perform a search against a mysql database and display the results on the same page. Want the ability to perform multiple search without a refresh or change of page.

so far this is what i have
Index.html
  1. <html>
  2. <head>
  3. <script src="selectuser.js"></script>
  4. </head>
  5. <body>
  6. <div align="center"
  7. <form name="form" action="search.php" method="get">
  8. <input type="text" name="q" />
  9. <input type="submit" name="Submit" value="Search" />
  10.  
  11. <div align="right">
  12. <select name="list">
  13. <option value="10"> 10 </option>
  14. <option value="20"> 20 </option>
  15. <option value="50"> 50 </option>
  16. </select>
  17. </div>
  18.  
  19. </form>
  20. </div>
  21. <div id="txtHint"><b>User info will be listed here.</b></div>
  22. </body>
  23. </html>

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

The code calls a search.php which performs the search against the database and creates the tables.

Search.php echos the tables using DIV .
Any ideas on how to improve this ?
Code so far is OK, only I do not think that this is a working search engine. This look almost exactly like code found here http://www.w3schools.com/PHP/php_ajax_database.asp, only a little bit modified.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 81
Reputation: it2051229 is an unknown quantity at this point 
Solved Threads: 1
it2051229 it2051229 is offline Offline
Junior Poster in Training

Re: PHP Ajax Search

 
0
  #4
Jan 17th, 2009
something is wrong on this area:

  1. <form name="form" action="search.php" method="get">
  2. <input type="text" name="q" />
  3. <input type="submit" name="Submit" value="Search" />

what you're doing there is you're refreshing the whole page when the search button is triggered which defeats the purpose of ajax. Now you created search.js which will be responsible for contacting the search.php BUT the problem is, your interface does not call the JS FUNCTION for connecting.

here's my suggestion



  1. <form name="myform">
  2. <input type="text" name="q" />
  3. <input type="button" name="button" value="Search" onclick="showUser()" />



now when the search button is click, it will call the showuser function from the javascript.... But there's still a problem, you need to pass the "NAME" to the server. At the moment the "NAME" is in the the text box and we're going to get that using javascript again.. so you might want to modify your searchUser function with something like:



  1. function showUser()
  2. {
  3. var str = document.myform.q.value;
  4.  
  5. xmlHttp=GetXmlHttpObject();
  6. if (xmlHttp==null)
  7. {
  8. alert ("Browser does not support HTTP Request")
  9. return
  10. }
  11.  
  12. //rest of the code here
  13. //...

you know AJAX is kindof long to explain, why dont you just follow the examples correctly provided on your tutorial? Because the way I see it you have syntax errors on your javascript.. oh and never ever do ajax if you don't know javascripting
Last edited by peter_budo; Jan 18th, 2009 at 2:09 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC