Syllabus Schedule Project Labs Canvas Server Access phpMyAdmin Assignment 1 Assignment 2 Assignment 3 Assignment 4 Assignment 5 Assignment 6 Assignment 7 Assignment 8 Assignment 9

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.

 <html>
   <head>
      <title>Sample web form</title>
      <link href="/sandvig/mis314/assignments/style.css" rel="stylesheet" type="text/css">
   </head>
   <body>
      <div class="pageContainer centerText">

         <h2>Hello Form</h2>
         <hr />

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

         <?php
         //Retrieve name from querystring. Check that parameter
         //is in querystring or may get "Undefined index" error
         if (isset($_GET['fname']))
         {
            $fname = $_GET['fname'];
            echo "<h1> Hello $fname";
         }
         ?>
      </div>
   </body>
</html>      
   


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.

2. GuessGame.php
  1. 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.
  2. For code readability, change the name of the input parameter from fname to something more appropriate, like "guessValue".
  3. 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)
    echo 'low.';
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 parameters from the querystring to local variables named $value1 and $value2. Use the is_numeric() function to check that the user entered numbers.
  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.


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.

Regular Expressions HTML Color Names Color Picker ASCII Character Codes Unicode Character Codes
Top