Assignment 2 -- Variables
You may find it useful to have your copy of Nixon available as a reference while
you work on the labs.
Store the files required for this assignment on Yorktown in a directory named A02.
This assignment requires the use of simple HTML forms. Please use the following code as
a template for exercises 1-4. You may cut-and-paste it into your php script.
<title>Sample web form</title>
<link href="/sandvig/mis314/assignments/style.css" rel="stylesheet" type="text/css">
<div class="pageContainer centerText">
<p>Please enter your name:</p>
<input type="text" name="fname" autofocus>
<input type="submit" value="Submit Name">
//Retrieve name from querystring. Check that parameter
//is in querystring or may get "Undefined index" error
$fname = $_GET['fname'];
echo "<h1> Hello $fname";
1. HelloForm.php -
Paste the code above into a file and name it HelloForm.php. This file
is a template for the other parts of the exercise.
- The user needs to enter a number for this exercise to work. Validate
the user's input by replacing the isset() function with the is_numeric() function.
For code readability, change the name of the input parameter
from fname to something more appropriate, like "guessValue".
- Use an if statement similar to the one in time greeting (source)
to give the user hints as they try to guess a number.
The if statement compares the user's input to the values shown in the following table and displays an
appropriate message. The statements look something like:
elseif ($intGuess < 7)
3. SimpleCalculator.php - Write a script that retrieves two values from
the querystring, adds them together and displays the results. Steps:
- 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).
- In your php code assign the parameters from the querystring to local variables named
$value1 and $value2. Use the is_numeric() function to check that
the user entered numbers.
- Echo the variables to the browser to make sure they have been retrieved correctly.
- 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:
- Look at the source code for the sample to see the image path for the die.
- 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:
- 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.
- Concatenate the values into the image paths for the two dice.
- Add the values together and display the sum.
Submission instructions: Submit assignments via
the Canvas course management system.
Submit the full URL for each exercise in the assignment, listing the URLs
in the same order that they are listed in the assignment. To minimize typos in URLs
it is strongly recommended
that you copy the URLs from the address bar of the browser
rather than trying to type them. Incorrect URLs
will not be graded and no credit will be given.
When pages are connected via navigation (as in your Bookstore project) it is only
necessary to submit the URL of the first page.