AJAX vraag
mijn javascript code:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<script type="text/javascript">
var xmlHttp
function GetXmlHttpObject(){
var xmlhttp = null;
if(window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
}else{
try{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}catch(e){
window.alert("Uw browser ondersteunt het XMLHttpRequest object niet!");
}
}
return xmlhttp;
}
function showName(str)
{
if (str.length==0)
{
document.getElementById("txtName").innerHTML="";
return;
}
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert ("Your browser does not support AJAX!");
return;
}
var url="getname.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)
{
document.getElementById("txtName").innerHTML=xmlHttp.responseText;
}
}
</script>
var xmlHttp
function GetXmlHttpObject(){
var xmlhttp = null;
if(window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
}else{
try{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}catch(e){
window.alert("Uw browser ondersteunt het XMLHttpRequest object niet!");
}
}
return xmlhttp;
}
function showName(str)
{
if (str.length==0)
{
document.getElementById("txtName").innerHTML="";
return;
}
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert ("Your browser does not support AJAX!");
return;
}
var url="getname.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)
{
document.getElementById("txtName").innerHTML=xmlHttp.responseText;
}
}
</script>
mijn php code:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?php
session_start();
include("config.php");
$q=$_GET["q"];
if (strlen($q) > 0)
{
$hint="";
$sql_g = mysql_query("SELECT nickname FROM users WHERE nickname LIKE '$q%'");
if (mysql_num_rows($sql_g) > 0)
{
while($a = mysql_fetch_array($sql_g)) {
$numer += 1;
if ($hint=="")
{
$hint=$a[nickname];
}
else
{
$hint=$hint." , ".$a[nickname];
}
}
}
}
if ($hint == "")
{
$response="Geen suggestie";
}
else
{
$response=$hint;
}
echo $response;
?>
session_start();
include("config.php");
$q=$_GET["q"];
if (strlen($q) > 0)
{
$hint="";
$sql_g = mysql_query("SELECT nickname FROM users WHERE nickname LIKE '$q%'");
if (mysql_num_rows($sql_g) > 0)
{
while($a = mysql_fetch_array($sql_g)) {
$numer += 1;
if ($hint=="")
{
$hint=$a[nickname];
}
else
{
$hint=$hint." , ".$a[nickname];
}
}
}
}
if ($hint == "")
{
$response="Geen suggestie";
}
else
{
$response=$hint;
}
echo $response;
?>
het html formulier:
Code (php)
1
2
3
4
5
2
3
4
5
<form action='profiles.php' method='post'>
<div id='txtName' style='100%; padding:5px; border:1px solid #000000'></div>
<tr><td>Naam:</td><td colspan='4'><input type="text" id="naam" name="naam" value="" onkeyup="showName(this.value)">
<br> <input type='submit' name='zoek' value='zoek'></td></tr>
</form>
<div id='txtName' style='100%; padding:5px; border:1px solid #000000'></div>
<tr><td>Naam:</td><td colspan='4'><input type="text" id="naam" name="naam" value="" onkeyup="showName(this.value)">
<br> <input type='submit' name='zoek' value='zoek'></td></tr>
</form>
weral IE, waarom gebruiken mensen dat eigenlijk nog???
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
function sendRequest(url,callback,postData) {
var req = createXMLHTTPObject();
if (!req) return;
var method = (postData) ? "POST" : "GET";
req.open(method,url,true);
req.setRequestHeader('User-Agent','XMLHTTP/1.0');
if (postData)
req.setRequestHeader('Content-type','application/x-www-form-urlencoded');
req.onreadystatechange = function () {
if (req.readyState != 4) return;
if (req.status != 200 && req.status != 304) {
// alert('HTTP error ' + req.status);
return;
}
callback(req);
}
if (req.readyState == 4) return;
req.send(postData);
}
var XMLHttpFactories = [
function () {return new XMLHttpRequest()},
function () {return new ActiveXObject("Msxml2.XMLHTTP")},
function () {return new ActiveXObject("Msxml3.XMLHTTP")},
function () {return new ActiveXObject("Microsoft.XMLHTTP")}
];
function createXMLHTTPObject() {
var xmlhttp = false;
for (var i=0;i<XMLHttpFactories.length;i++) {
try {
xmlhttp = XMLHttpFactories[i]();
}
catch (e) {
continue;
}
break;
}
return xmlhttp;
}
var req = createXMLHTTPObject();
if (!req) return;
var method = (postData) ? "POST" : "GET";
req.open(method,url,true);
req.setRequestHeader('User-Agent','XMLHTTP/1.0');
if (postData)
req.setRequestHeader('Content-type','application/x-www-form-urlencoded');
req.onreadystatechange = function () {
if (req.readyState != 4) return;
if (req.status != 200 && req.status != 304) {
// alert('HTTP error ' + req.status);
return;
}
callback(req);
}
if (req.readyState == 4) return;
req.send(postData);
}
var XMLHttpFactories = [
function () {return new XMLHttpRequest()},
function () {return new ActiveXObject("Msxml2.XMLHTTP")},
function () {return new ActiveXObject("Msxml3.XMLHTTP")},
function () {return new ActiveXObject("Microsoft.XMLHTTP")}
];
function createXMLHTTPObject() {
var xmlhttp = false;
for (var i=0;i<XMLHttpFactories.length;i++) {
try {
xmlhttp = XMLHttpFactories[i]();
}
catch (e) {
continue;
}
break;
}
return xmlhttp;
}
ik zal het wel proberen.