Trying to get data from php server. Data shows file itself. no errors are shown in console.
function process() {
if (xmlHttp.readyState == 0 || xmlHttp.readyState == 4) { // 0 and 4 if object is ready to go and not busy
food = encodeURIComponent(document.getElementById("userInput").value); //document is web page
// to communicate with server
xmlHttp.open("GET", "foodstore.php?food=" + food, true); //create we wana sent to server, request type should be same, uri is second parameter, true is request to be handled asynchrously
xmlHttp.onreadystatechange = handleServerResponse; //3.handle that request,like update a page or something
xmlHttp.send(); //if GET then null
} else {
setTimeout(process, 1000);
//if busy timeout then communicate again
}
}
foodstore.php
using array for foodItems and checking if alphabets are typed in input box.
<?php
header('Content-Type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>';
//response stored in food
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 ass';
else
echo 'Sorry we dont sell no ' .$food.'!';
echo '</response>';
?>