(Assignment last updated: 1/18/2008 3:16:34 PM)  

Assignment 2 -- Variables

You may find it useful to have your copy of Sklar available as a reference while you work on the labs. 

Store the files required for this assignment in your Aspen account in a directory named A02.

This assignment requires the use of simple HTML forms. The following code for a simple form can be cut-and-pasted into Aspen.

<html>
<body style="text-align:center">
<h3>Hello Form</h3><hr />

<form>
   <p>
   Please enter your name:
   <input type="text" name="FirstName">
   <input type="submit" value="Submit Name"></p>
</form>

<?
//Retrieve name from querystring and assign to a local variable
$strFirstName = $_GET['FirstName'];

//Print name if string length > 0
if (strlen($strFirstName) > 0)
{
echo "<h1> Hello $strFirstName";
}
?>
</body>
</html>

 


1. HelloForm.php - Use Aspen to paste the code above into a file on and name it HelloForm.php.

2. GuessGame.php - Use an if statement similar to the one in time greeting to give the user hints as they try to guess a number: 

Guess    Response
<4   very low
4-6   Low
7   Correct!
8-10   High
> 10   very high

3. SimpleCalculator.php - Write a script that retrieves two values from the querystring, adds them together and displays the results. Steps:

  1. Copy the code from HelloForm.php and modify it to display two text boxes. Assign the values entered to variables named value1 and value2 (tip: look at the source code for the example).
  2. In your php code assign the values from the querystring to local variables named $value1 and $value2.
  3. Echo the variables to the browser to make sure they have been retrieved correctly.
  4. Add and print the values with the statement:
    echo 'Sum is: ' . ($Value1 + $Value2);

4. DisplayImage.php - It is quite common to display images by concatenating dynamic information into the image path. For instance, Amazon.com concatenates book ISBNs into its image paths to display specific book images. In this exercise you will display an image of a die by concatenating a value from a textbox into the image path. Steps:

  1. Look at the source code for the sample to see the image path for the die.
  2. Put the html for the image into the echo statement. The statement starts like this:
       echo "<img src='/sandvig/...

5. RandomImage.php - It is easy to generate random numbers in PHP. Display two die and their sum. Steps:

  1. Generate two random numbers between 1 and 6 using the rand() function. Google "php rand" for the documentation. Assign each random number to a variable.
  2. Concatenate the values into the image paths for the two dice.
  3. Add the values together and display the sum.

Check your URLs to make sure they are correct before you submit them for grading.  The subject line of your email should read "MIS314 Ax YourName" where x is the assignment number. Send an email with the appropriate URLs to: 

  1. Professor Sandvig at (note: this address is for homework assignments only. Please send other email to )
  2. The class teaching assistant, Emma James, at
  3. cc a copy to yourself (as a time-stamped receipt of your submission) 

When pages are connected via navigation it is only necessary to submit the URL to the first page.