Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have to change this Ajax code to have it provide the addresses of repeat custo

ID: 3638076 • Letter: I

Question

I have to change this Ajax code to have it provide the addresses of repeat customers using both names and addresses as the hash. I have the HTML, and JS and PHP code - I have the request functions and response funtions keyed - but don't know what kind of file to put them in - js? - do they need to be named - how are they utilized to get the result - Here's my code my category:

HTML CODE:

<?xml version = "1.0" encoding = "utf-8" ? >
<!D
OCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http//www.w3.org/TR/xhtmlll/DTD/xhtmll-strict.dtd">

<!--popcornA.html

-->
<html xmlns = "http://www.w3.org/1999/xhtml">
<head> <title> Popcorn Sales Form (AJax) </title>
<style type = "text/css">
img {position: absolute;left: 400px; top: 50px;}
</style>
<script type = "text/Javascript" src="popcornA.js">
</script>
</head>
<body>
<h2> Welcome to Millennium Gymnastics Booster Club Popcorn
Sales
</h2>
<form action = "">
<!-- A borderless table of text widgets for name and address -->
<table>
<tr>
<td> Buyer's Name: </td>
<td> <input type = "text" name = "name" size = "30" />
</td>
</tr>
<tr>
<td> Street Address: </td>
<td> <input type = "text" name = "street" size = "30" />
</td>
</tr>
<tr>
<td> Zip code: </td>
<td> <input type = "text" name = "zip" size = "10"
/>
</td>
</tr>
<tr>
<td> City </td>
<td> <input type = "text" name = "city" id = "city" size = "30" />
</td>
</tr>
<tr>
<td> State </td>
<td> <input type = "text" name = "state" id = "state" size = "30" />
</td>
</tr>
</table>
<img src="popcorn.jpg" alt = "picture of popcorn" />
</p>
<!-- The submit and reset buttons -->
<p>
<input type = "submit" value = "Submit Order" />
<input type = "reset" value = "Clear Order Form" />
</p>
</form>
</body>
</html>

JS Code:

// popcornA.js<
function getPlace(zip) {

var xhr;

// Get the object for all browsers except IE5 and IE6
if (window.XMLHttpRequest)
xhr = new XMLHttpRequest();

Otherwise get the object for IE5 and IE6
else
xhr = new ActiveXObject("Microsoft.XMLHTTP");

// Register the embedded receiver function as the handler
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
var result = xhr.responseText;
alert("result = " + result);
var place = result.split(', ');
if (document.getElementById("city").value == "")
document.getElementById("city").value = place[0];
if (document.getElementById("state").value == "")
document.getElementById("state").value = place[1];
}
}
xhr.open("GET", "getCityState.php?zip=" + zip, true);
xhr.send(null);
}


PHP Code:

<?php

//getCityState.php
$cityState = array("81611" => "Aspen, Colorado",
"81411" => "Bedrock, Colorado",
"80908" => "Black Forest, Colorado",
"80301" => "Boulder, Colorado",
"81127" => "Chimney Rock, Colorado",
"80901" => "Colorado Springs, Colorado",
"81223" => "Cotopaxi, Colorado",
"80201" => "Denver Colorado",
"81657" => "Vail, Colorado",
"80435" => "Keystone, Colorado",
"80536" => "Virginia Dale, Colorado"
);
header("content-type: text/plain");
$zip = $_GET["zip"];
if (array_key_exists($zip, $cityState))
print $cityState[$zip];
else
print " , ";
?>


Here are the three functions that I don't know where to put and whteher that should be js files:

Request Handler Function:

function getPlace(zip) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = receivePlace;
xhr.open("GET", "getCityState.php?zip=" +zip, true);
xhr.send(null);


Response function number one:

function () {
if (xhr.readyState == 4 && status == 200) {
var result = xhr.responseText;
var place = result.split(', ');
if (document.getElementById("city").value== "")
document.getElementById("city").value = place[0];
if (document.getElementById("state").value == "")
document.getElementById("state").value = place[1];
}
}

Response function number 2:

function getPlace(zip) {
if (window.XMLHttpRequest)
xhr = new XMLHttpRequest();
else
xhr = new ActiveXObject("Microsoft.XMLHTTP");
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
var result = xhr.responseText;
var place = result.split(', ');
if (document.getElementById("city").value == "")
document.getElementById("city").value = place[0];
if (document.getElementById("state").value == "")
document.getElementById("state").value = place[1];
}
}
xhr.open("GET", "getCityState.php?zip=" + zip);
xhr.send(null);
}






Explanation / Answer

No answer text found in this record.