943,186 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 749
  • PHP RSS
Jun 26th, 2010
0

Ajax in my php

Expand Post »
High guyz am tryin to get hint from my database but i am managing to do so,though if i list the array am able. please help if any one has an idea

here is the html code
PHP Syntax (Toggle Plain Text)
  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4. function showHint(str)
  5. {
  6. if (str.length==0)
  7. {
  8. document.getElementById("txtHint").innerHTML="";
  9. return;
  10. }
  11. if (window.XMLHttpRequest)
  12. {// code for IE7+, Firefox, Chrome, Opera, Safari
  13. xmlhttp=new XMLHttpRequest();
  14. }
  15. else
  16. {// code for IE6, IE5
  17. xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  18. }
  19. xmlhttp.onreadystatechange=function()
  20. {
  21. if (xmlhttp.readyState==4 && xmlhttp.status==200)
  22. {
  23. document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
  24. }
  25. }
  26. xmlhttp.open("GET","gethint.php?q="+str,true);
  27. xmlhttp.send();
  28. }
  29. </script>
  30. </head
  31. <body>
  32.  
  33. <p><b>Start typing a name in the input field below:</b></p>
  34. <form>
  35. First name: <input type="text" onkeyup="showHint(this.value)" size="20" />
  36. </form>
  37. <p>Suggestions: <span id="txtHint"></span></p>
  38.  
  39. </body>
  40. </html>

and the php code looks like this
PHP Syntax (Toggle Plain Text)
  1. <?php
  2. // Fill up array with names
  3. $a[]="Anna";
  4. $a[]="Brittany";
  5. $a[]="Cinderella";
  6. $a[]="Diana";
  7. $a[]="Eva";
  8. $a[]="Fiona";
  9. $a[]="Gunda";
  10. $a[]="Hege";
  11. $a[]="Inga";
  12. $a[]="Johanna";
  13. $a[]="Kitty";
  14. $a[]="Linda";
  15. $a[]="Nina";
  16. $a[]="Ophelia";
  17. $a[]="Petunia";
  18. $a[]="Amanda";
  19. $a[]="Raquel";
  20. $a[]="Cindy";
  21. $a[]="Doris";
  22. $a[]="Eve";
  23. $a[]="Evita";
  24. $a[]="Sunniva";
  25. $a[]="Tove";
  26. $a[]="Unni";
  27. $a[]="Violet";
  28. $a[]="Liza";
  29. $a[]="Elizabeth";
  30. $a[]="Ellen";
  31. $a[]="Wenche";
  32. $a[]="Vicky";
  33.  
  34. //get the q parameter from URL
  35. $q=$_GET["q"];
  36.  
  37. //lookup all hints from array if length of q>0
  38. if (strlen($q) > 0)
  39. {
  40. $hint="";
  41. for($i=0; $i<count($a); $i++)
  42. {
  43. if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q))))
  44. {
  45. if ($hint=="")
  46. {
  47. $hint=$a[$i];
  48. }
  49. else
  50. {
  51. $hint=$hint." , ".$a[$i];
  52. }
  53. }
  54. }
  55. }
  56.  
  57. // Set output to "no suggestion" if no hint were found
  58. // or to the correct values
  59. if ($hint == "")
  60. {
  61. $response="no suggestion";
  62. }
  63. else
  64. {
  65. $response=$hint;
  66. }
  67.  
  68. //output the response
  69. echo $response;
  70. ?>
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Jothe is offline Offline
22 posts
since Jun 2010
Jun 26th, 2010
0
Re: Ajax in my php
hehe your ajax example is from w3schools...

ok first make a database for names, then query them all

then changed this part from an array

PHP Syntax (Toggle Plain Text)
  1. // Fill up array with names
  2. $a[]="Anna";
  3. $a[]="Brittany";
  4. $a[]="Cinderella";
  5. $a[]="Diana";
  6. $a[]="Eva";
  7. $a[]="Fiona";
  8. $a[]="Gunda";
  9. $a[]="Hege";
  10. $a[]="Inga";
  11. $a[]="Johanna";
  12. $a[]="Kitty";
  13. $a[]="Linda";
  14. $a[]="Nina";
  15. $a[]="Ophelia";
  16. $a[]="Petunia";
  17. $a[]="Amanda";
  18. $a[]="Raquel";
  19. $a[]="Cindy";
  20. $a[]="Doris";
  21. $a[]="Eve";
  22. $a[]="Evita";
  23. $a[]="Sunniva";
  24. $a[]="Tove";
  25. $a[]="Unni";
  26. $a[]="Violet";
  27. $a[]="Liza";
  28. $a[]="Elizabeth";
  29. $a[]="Ellen";
  30. $a[]="Wenche";
  31. $a[]="Vicky";

to

PHP Syntax (Toggle Plain Text)
  1. $sql = mysql_query("SELECT name FROM users"); //sample table users
  2. while($row=mysql_fetch_array($sql)){
  3. $a[] = $row['name'];
  4. }
Last edited by vaultdweller123; Jun 26th, 2010 at 8:10 am.
Reputation Points: 32
Solved Threads: 60
Posting Pro in Training
vaultdweller123 is offline Offline
484 posts
since Sep 2009
Jun 26th, 2010
0
Re: Ajax in my php
First of all please rephrase your question.

Secondly, I recommend to use jQuery instead of reinventing the wheel again in using ajax.

Thirdly, i believe that w3schools ajax code is not correct since it was not working with me under some circumstances. Change the line 27 from

PHP Syntax (Toggle Plain Text)
  1. xmlhttp.send(); to xmlhttp.send(NULL);
Reputation Points: 17
Solved Threads: 14
Junior Poster in Training
shubhamjain1 is offline Offline
56 posts
since Oct 2009
Jun 28th, 2010
0
Re: Ajax in my php
@vaultdweller123 thanx the code worked...and yes code is from W3schools.
@shubhamjain1 the code for w3schools works alright but you need to understand it, in this case if you changed the last part of the code
PHP Syntax (Toggle Plain Text)
  1. xmlhttp.send();
to
PHP Syntax (Toggle Plain Text)
  1. xmlhttp.send(arguments);
it will work under all circumstances..thanx anyway
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Jothe is offline Offline
22 posts
since Jun 2010
Jun 28th, 2010
0
Re: Ajax in my php
@vaultdweller123 tha code worked for a few minutes when i came back it gave me the following error:
mysql_fetch_array() expects parameter 1 to be resource, boolean. the warning is shown at the following line
PHP Syntax (Toggle Plain Text)
  1. while($row=mysql_fetch_array($query, $db) or die(mysql_error))
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Jothe is offline Offline
22 posts
since Jun 2010
Jun 28th, 2010
0
Re: Ajax in my php
just stick to my original code and you'll do fine

PHP Syntax (Toggle Plain Text)
  1. $sql = mysql_query("SELECT name FROM users"); //sample table users
  2. while($row=mysql_fetch_array($sql)){
  3. $a[] = $row['name'];
  4. }
Last edited by vaultdweller123; Jun 28th, 2010 at 10:29 am.
Reputation Points: 32
Solved Threads: 60
Posting Pro in Training
vaultdweller123 is offline Offline
484 posts
since Sep 2009
Jun 29th, 2010
0
Re: Ajax in my php
thanx ur code helped me alot tho it didn't work as surposed so i changed some part of code and it worked..i changed it to
PHP Syntax (Toggle Plain Text)
  1. $query = 'SELECT name FROM ecomm_products ORDER BY product_code';
  2. $result= mysql_query($query, $db) or die(mysql_error($db));
  3. while($row =mysql_fetch_assoc($result)){
  4. foreach($row as $value){
  5. $a[] = $row['name'];
  6. }
  7. }
thanx anyway
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Jothe is offline Offline
22 posts
since Jun 2010

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: Values from for loop into database
Next Thread in PHP Forum Timeline: How to Save Value Multiple Dropdown





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


Follow us on Twitter


© 2011 DaniWeb® LLC