This is my Html code ,which calls on process() function with foodstore.js as it's attached javascript file
<script type="text/javascript" src="foodstore.js"> </script>
</head>
<body onload="process()">
<h3>The Chuff Bucket</h3>
Enter the food you would like to order:
<input type="text" id="userInput">
<div id="underInput"> </div>
</body>
</head>
This is my php code
header('Content-Type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>';
echo '<response>';
$food = $_GET['food'];
$foodArray = array('tuna','bacon','beef','loaf','ham');
if(in_array($food,$foodArray))
echo 'We do have '.$food.'!';
elseif($food=='')
echo 'Enter a food you idiot';
else
echo 'Sorry punk we dont sell no ' .$food.'!';
echo '</response>';
?>
This is my java script code
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("cant create that object hoss!");
else
return xmlHttp;
}
function process(){
if(xmlHttp.readyState==0 || xmlHttp.readyState==4)
{
food=encodeURIComponent(document.getElementById("userInput").value );
xmlHttp.open("GET", "foodstore.php?food="+food, true);
xmlHttp.onreadystatechange = handleServerResponse;
xmlHttp.send(null);
}
else
{
setTimeout('process()', 1000);
}
}
function handleServerResponse(){
if(xmlHttp.readyState==4){
if(xmlHttp.status==200){
alert("hi");
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!');
}
}
}
I feel the error is mostly to do with "handleServerResponse" function ,Any help would be highly appreciated!