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

<?php // getCityState.php // Gets the form value from the \"zip\" widget, looks

ID: 3816326 • Letter: #

Question


<?php
// getCityState.php
// Gets the form value from the "zip" widget, looks up the
// city and state for that zip code, and prints it for the
// form
  
   //array that holds some city and state values
   $cityState = array(
       "21401"=> "Annapolis, MD",
       "21402" => "Annapolis, MD",
       "21044" => "Columbia, MD",
       "21045" => "Columbia, MD",
       "21221" => "Baltimore, MD",
       "21222" => "Baltimore, MD"
   );
  
//retrieve value of get parameter
$zip = $_GET["zip"];
  
//check if zip value exists in array above, and retrieve the city, state values corresponding to the matching zip value
if (array_key_exists($zip, $cityState))
print $cityState[$zip];
else
   //if doesn't exist, just give an error message
print "no matching entry, no matching entry";
?>
function getPlace(valueOfZipCode){
   //create a new Ajax request to the URL getCityState.php
   //query-string-parameter-name is zip
   //query-string-parameter-value is the parameter that was sent to this function, i.e., the zip code value that the user entered
   //on the successful processing of the Ajax request, we want the displayCityState function to be invoked
new Ajax.Request( "getCityState.php",
{
method: "get",
parameters: {zip:valueOfZipCode},
onSuccess: displayCityState
} );
}
//displayCityState is the function that is to be executed when the Ajax request is successful
//IMPORTANT: The variable ajax MUST be in the parameter list of this function
//this function, retrieves the response sent by the PHP program above, splits the response at the comma-space character
//and populates the values of the city and state textboxes with the split values
function displayCityState(ajax){
var result = ajax.responseText;
var place = result.split(', ');
$("city").value = place[0];
$("state").value = place[1];
}
<?xml version = "1.0" encoding = "utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<!-- popcornA.html
This describes popcorn sales form page which uses
Ajax and the zip code to fill in the city and state
of the customer's address
-->
<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>
   <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/prototype/1.6.0.3/prototype.js"></script>
</head>
<body>
<h2> Welcome to Millenium Gynmastics 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>

I am trying to run this code using prototype toolkit, but when wever I enter the zip code that is placed in the array list, I keep gettiing "undefined" in state and no calue in city. This code is suppose to populate city and state when I enter the zip code. But it's not working and I can't find any issues in it. This code contains the php, js, and html, file.

Explanation / Answer

Your code is fine, I am explaining here why it did not work.

Your code did not work in my system firstly because the file name you have pasted here "getCityState.php" may have some invalid characters. That's why apache server refused to identify any file with such name. Then I renamed the file by typing file name manually. Then It started working. I would suggest you to do the same. After these corrections, your webpage is working perfectly fine, I entered zip code 21401 and pressed tab and it did retrieve Annapolis and MD.

You will have to take care of couple of things.

First

Save the code below in getCityState.php (IMPORTANT : Copy this file name from here and paste as file name in your system If the problem persist then Rename the file and type file name manually)

<?php
// getCityState.php
// Gets the form value from the "zip" widget, looks up the
// city and state for that zip code, and prints it for the
// form
  
//array that holds some city and state values
$cityState = array(
"21401"=> "Annapolis, MD",
"21402" => "Annapolis, MD",
"21044" => "Columbia, MD",
"21045" => "Columbia, MD",
"21221" => "Baltimore, MD",
"21222" => "Baltimore, MD"
);
  
//retrieve value of get parameter
$zip = $_GET["zip"];
  
//check if zip value exists in array above, and retrieve the city, state values corresponding to the matching zip value
if (array_key_exists($zip, $cityState))
print $cityState[$zip];
else
//if doesn't exist, just give an error message
print "no matching entry, no matching entry";
?>

Second

There should not be any comment touching to <?php tag.

e.g. for invalid php tag

<?php//some comment

e.g. for valid php tag

<?php

//some comment

If you have any question regarding this solution then please do comment.
Good luck.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote