Hallo liebes Forum
Ich habe irgendein Problem mit meinem Ajax Code, denn er functioniert nicht ;D
kukt es sich einer bitte an.
über die Lösung würd ich mich freun!
index.php:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="connect.js"></script>
<style type="text/css">
</style>
</head>
<body onload="process()">
<h3>Ajax</h3>
Enter a Food:
<input type="text" id="userInput"/>
<div id="underInput"></div>
</body>
</html>
connect.js:
var xmlHttp = createXmlHttpRequestObject();
function createXmlHttpRequestObject(){
var xmlHttp;
if(window.ActiveXObject){
try{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}catch(e){
xmlHttp = false;
}
}else{
try{
xmlHttp = new XMLHttpRequest();
}catch(e){
xmlHttp = false;
}
}
if(!xmlHttp){
alert("Ajax geht ned");
}else{
return xmlHttp;
}
}
function process(){
if(xmlHttp.readyState===0 || xmlHttp.readyState==4){
food = encodeURIComponent(document.getElementById("userInput").value);
xmlHttp.open("GET","contact.php?food=" + food,true);
xmlHttp.onreadystatechange = handleServerResponse();
xmlHttp.send(null);
}else{
setTimeout(process(),1000);
}
}
function handleServerResponse(){
if(xmlHttp.readyState==4){
if(xmlHttp.status==200){
xmlResponse = xmlHttp.responseXML;
xmlDocumentElement = xmlResponse.documentElement;
message = xmlDocumentElement.firstChild.data;
document.getElementById("underInput").innerHTML ="<span style='color:blue'>" + message + "</span>";
setTimeout(process(),1000);
}else{
alert('Something went wrong!');
}
}
}
contact.php:
<?php
header('Content-Type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>';
echo '<response>';
$food = $_GET["food"];
$foodArray = array('tuna','beef','bacon','ham');
if(in_array($food,$foodArray)){
echo 'Enter a Food';
}elseif($food==""){
echo 'Gib was ein';
}else{
echo 'Sorry wir konnten '.$food.'nicht finden !';}
echo '</response>';
?>