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
Alles anzeigen
ajax.js:
Alles anzeigen
index.php
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
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
- <?php
- //get the q parameter from URL
- $q=$_GET["q"];
- $a[] = "Rouven";
- $a[] = "Reinhold";
- $a[] = "Markus";
- $a[] = "Alexander";
- $a[] = "Werner";
- $a[] = "Klaus";
- $a[] = "Peter";
- $a[] = "Franz";
- //lookup all hints from array if length of q>0
- if (strlen($q) > 0)
- {
- $hint="";
- for($i=0; $i<count($a); $i++)
- {
- if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q))))
- {
- if ($hint=="")
- {
- $hint=$a[$i];
- }
- else
- {
- $hint=$hint." , ".$a[$i];
- }
- }
- }
- }
- // Set output to "no suggestion" if no hint where found
- // or to the correct values
- if ($hint == "")
- {
- $response="no suggestion";
- }
- else
- {
- $response=$hint;
- }
- //output the response
- echo $response;
- ?>
ajax.js:
Quellcode
- var xmlHttp
- function showHint(str)
- {
- if (str.length==0)
- {
- document.getElementById("txtHint").innerHTML=""
- return
- }
- xmlHttp=GetXmlHttpObject()
- if (xmlHttp==null)
- {
- alert ("Browser does not support HTTP Request")
- return
- }
- var url="source.php"
- url=url+"?q="+str
- url=url+"&sid="+Math.random()
- xmlHttp.onreadystatechange=stateChanged
- xmlHttp.open("GET",url,true)
- xmlHttp.send(null)
- }
- function stateChanged()
- {
- if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
- {
- document.getElementById("txtHint").innerHTML=xmlHttp.responseText
- }
- }
- function GetXmlHttpObject()
- {
- var objXMLHttp=null
- if (window.XMLHttpRequest)
- {
- objXMLHttp=new XMLHttpRequest()
- }
- else if (window.ActiveXObject)
- {
- objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
- }
- return objXMLHttp
- }
index.php
Quellcode
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