CS1102 Lab 12

Last week's work

Unfortunately, some of you didn't take last week's work seriously. I had hoped that you were motivated enough to tackle the exercises even though they did not have to be submitted. I guess in some cases I was mistaken. Mea culpa.

Accordingly, I require you to submit guy.php from lab 11. If you did not finish it, then do so in your own time. Submit guy.php (not a zip file) in the usual way, selecting LAB11 when prompted. Deadline: 1 pm, Tuesday 5th February.

This week's work

Inside your public_html/cs1102 folder, create a subdirectory called lab12 (all lowercase) to hold this week's work.

How's the form?

Let's start with something very simple.

  1. Create a file called dec.html based on template.html. (NB: .html, not .php). Viewed in a browser, this file should display a form comprising just one label, one text-field and a submit button. The user will type a decimal number into the text-field. When s/he presses the submit button, his/her data will be sent to the server (using the GET method), where it will be processed by a PHP script called dec2bin.php.
  2. Now create dec2bin.php based on template.php. (NB: .php, not .html).
    1. This script will first obtain the user's decimal number, that was sent to the server. (Remember, there is a predefined associative array called $_GET that makes this available to you.)
    2. Then it converts the user's decimal number to binary. Don't worry! This is is easy because PHP offers a predefined function for this. It is called decbin. Suppose n is an integer in decimal. Then decbin(n) converts n to binary and returns the result. E.g. decbin(8) returns 1000.
    3. Use echo to write both the user's decimal number and the corresponding binary number to the output.

A more professional version of this program would check the user's input. If s/he fails to type a decimal number, e.g. s/he types "hello Mum", the script would display an error message. You need not bother with this, unless you particularly want to.

Rock-Scissors-Paper

Create an XHTML web page in a file rsp.html and a PHP script in a file called rsp.php.

The web page displays a form containing three radio buttons, labelled Rock, Scissors and Paper. It submits the user's data using the GET method to the PHP script, rsp.php

The PHP script

  1. generates a random number, 0, 1 or 2, where 0 means Rock, 1 means Scissors and 2 means Paper;
  2. uses if statements to compare what the system has generated with what the user has selected, where
  3. outputs the outcome using a sentence such as "My Rock beats your Scissors", or "Your Paper beats my Rock", or "My Rock ties with your Rock", etc.

Zip your lab12 folder and submit lab12.zip in the usual way, selecting LAB12 when asked.

Deadline: 1pm, Tuesday 5th February.