AJAX und MySQL

  • AJAX und MySQL

    Hallo,

    ich habe folgendes Problem:

    Wenn ich in einem Textfeld, einen Buchstaben eingebe, soll ajax automatisch in der mysql-datenbank nach den einträgen suchen, in denen der Buchstabe vorkommt und dann ausgeben.
    Soweit so gut. Ohne Datenbankabfrage klappt alles einwandfrei. Mit geht gar nichts.

    Quelltext:

    source.php

    Quellcode

    1. <?php
    2. //get the q parameter from URL
    3. $q=$_GET["q"];
    4. $a[] = "Rouven";
    5. $a[] = "Reinhold";
    6. $a[] = "Markus";
    7. $a[] = "Alexander";
    8. $a[] = "Werner";
    9. $a[] = "Klaus";
    10. $a[] = "Peter";
    11. $a[] = "Franz";
    12. //lookup all hints from array if length of q>0
    13. if (strlen($q) > 0)
    14. {
    15. $hint="";
    16. for($i=0; $i<count($a); $i++)
    17. {
    18. if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q))))
    19. {
    20. if ($hint=="")
    21. {
    22. $hint=$a[$i];
    23. }
    24. else
    25. {
    26. $hint=$hint." , ".$a[$i];
    27. }
    28. }
    29. }
    30. }
    31. // Set output to "no suggestion" if no hint where found
    32. // or to the correct values
    33. if ($hint == "")
    34. {
    35. $response="no suggestion";
    36. }
    37. else
    38. {
    39. $response=$hint;
    40. }
    41. //output the response
    42. echo $response;
    43. ?>
    Alles anzeigen



    ajax.js:

    Quellcode

    1. var xmlHttp
    2. function showHint(str)
    3. {
    4. if (str.length==0)
    5. {
    6. document.getElementById("txtHint").innerHTML=""
    7. return
    8. }
    9. xmlHttp=GetXmlHttpObject()
    10. if (xmlHttp==null)
    11. {
    12. alert ("Browser does not support HTTP Request")
    13. return
    14. }
    15. var url="source.php"
    16. url=url+"?q="+str
    17. url=url+"&sid="+Math.random()
    18. xmlHttp.onreadystatechange=stateChanged
    19. xmlHttp.open("GET",url,true)
    20. xmlHttp.send(null)
    21. }
    22. function stateChanged()
    23. {
    24. if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
    25. {
    26. document.getElementById("txtHint").innerHTML=xmlHttp.responseText
    27. }
    28. }
    29. function GetXmlHttpObject()
    30. {
    31. var objXMLHttp=null
    32. if (window.XMLHttpRequest)
    33. {
    34. objXMLHttp=new XMLHttpRequest()
    35. }
    36. else if (window.ActiveXObject)
    37. {
    38. objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
    39. }
    40. return objXMLHttp
    41. }
    Alles anzeigen


    index.php

    Quellcode

    1. <html>
    2. <head>
    3. <script src="ajax.js"></script>
    4. </head>
    5. <body>
    6. <form>
    7. First Name:
    8. <input type="text" id="txt1"
    9. onkeyup="showHint(this.value)">
    10. </form>
    11. <p>Suggestions: <span id="txtHint"></span></p>
    12. </body>
    13. </html>
    Alles anzeigen


    Nunja, wenn ich nun in der source.php statt $a[] = "..."; ne DB-Abfrage haben will, klappt gar nichts mehr.

    Woran könnte das liegen bzw. wie lässt sich das realisieren, dass das klappt?

    Gruß,
    TheRouv
  • sorry...hab bei der abfrage was vergessen gehabt...

    Quellcode

    1. <?php
    2. //get the q parameter from URL
    3. $q=$_GET["q"];
    4. // MySQL-Abfrage
    5. $sql = "SELECT feld FROM tabelle WHERE feld LIKE '%$q%'";
    6. $result = mysql_query($sql);
    7. ?>


    hatte die "%"-Zeichen vergessen...und dann ist es klar, dass er nicht überprüft, ob der buchstabe in dem betreffenden namen vorkommt....
    aber jetz funzt es...

    danke d0nUt...hast mich meinen quellcode genauers anschauen lassen...

    Gruß,
    TheRouv


    PS:
    super seite ;)