|
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:
- 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 values from the querystring to local
variables named $value1 and $value2.
- 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.
|