In need of the AJAX code.

+1 Paul Pops · January 20, 2015
Is there a place to get the code to this AJAX tutorial up through lesson 11?

I did the first part and it did not interact.

This is what I have and I don't have the code to check it with.

<?php
header('Content-Type: text/xml');
echo '<?xml version="1.0" encoding="UT-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 please';
else
echo 'Sorry friend, we dont sell no '.$food.'!';

echo '</response>';

?>

// JavaScript Document

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.");
else
return xmlHttp;
}

function process(){
if(xmlHttp.readyState==4 || xmlHttp.readyState==4) {
food = encodeURIComponent(document.getElementById("userInput").value);
xmlHttp.open("GET", "foodstore.php?food=" + food, true); 
xml.onreadystatechange = handleServerResponse;
xmlHttp.send(null);

}else{
setTimeout('process()',1000);
}

}

function handleServerResponse() {
if(xmlHttp.readyState==4) {
if(xmlHttp.status==200) {
xmlResponse = smlHttp.responseXML;
xmlDocumentElement = smlResponse.documentElement;
message = xmlDocumentElement.firstChild.data;
document.getElementById("underInput").innerHTML = '<span style="color:blue">' + message + '</span>';
setTimeout('process()',1000);

}else{
alert('something went wrong!');
}
}
}

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<script type="text/javascript" src="foodstore.js"></script>

</head>

<body onload="process()">

<h3>The Food Basket</h3>
Enter the food you would like to order:
<input type="text" id="userInput" />

<div id="underInput" />
</body>
</html>



Post a Reply

Replies

Oldest  Newest  Rating
0 Inzamam Tahir · January 21, 2015
Well, I did not read the whole code though but I did get the idea that you want the complete working code up to tutorial 11 and you can get that by visiting the link below

http://tzachsolomon.blogspot.com/2013/02/ajax-first-example-based-on-new-boston.html?showComment=1421844189177#c18858323436894657

I hope this is exactly what you want.
0 Paul Pops · January 22, 2015
Thanks Inzaman!

Once I get the hang of the stuff up to 11, I will hopefully be able to move on to 12 and beyond. 
The correct code will help the learning process.
The videos so far have been very good.  
0 Paul Pops · January 22, 2015
The code is working! Thank you! 

I found a small spelling error in the "Microsofot.XMLHTTP"            JS page. It doesn't affect the outcome.
0 Paul Pops · January 25, 2015
Inzamam, or anyone?

Could you please post the correct code for AJAX tutorials  18 - 20 if you have it.
It is the part that covers "Things in my pocket" with CSS and Javascript.
thanks.
  • 1

Javascript

107,265 followers
About

A scripting language that is added to standard HTML to create interactive effects, apps, games for the browser.

Links
Moderators