943,693 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 21014
  • PHP RSS
Apr 9th, 2007
0

PHP Ajax Search

Expand 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
PHP Syntax (Toggle Plain Text)
  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
PHP Syntax (Toggle Plain Text)
  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 ?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
BeerGuzler is offline Offline
4 posts
since Apr 2007
Apr 11th, 2007
0

Re: PHP Ajax Search

Click to Expand / Collapse  Quote originally posted by BeerGuzler ...
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
PHP Syntax (Toggle Plain Text)
  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
PHP Syntax (Toggle Plain Text)
  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
PHP Syntax (Toggle Plain Text)
  1. var xmlHttpfunction showUser(str)
  2. {

should be

PHP Syntax (Toggle Plain Text)
  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.

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

I believe you want:

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

or simpler:

PHP Syntax (Toggle Plain Text)
  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.
Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005
Jan 17th, 2009
0

Re: PHP Ajax Search

Click to Expand / Collapse  Quote originally posted by BeerGuzler ...
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
PHP Syntax (Toggle Plain Text)
  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
PHP Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Cybertronics is offline Offline
1 posts
since Jan 2009
Jan 17th, 2009
0

Re: PHP Ajax Search

something is wrong on this area:

html Syntax (Toggle Plain Text)
  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



html Syntax (Toggle Plain Text)
  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:



JavaScript Syntax (Toggle Plain Text)
  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.
Reputation Points: 7
Solved Threads: 1
Junior Poster in Training
it2051229 is offline Offline
82 posts
since May 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Problem with dynamic paths
Next Thread in PHP Forum Timeline: Any one who can help me out in PHP





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC