Lab 3

Lab 3

Forms & PHP Intro


Goals

Learn about...

  1. HTML From Elements
  2. Form Submission Concept
  3. Connecting Forms to PHP scripts
  4. GET and POST methods

Setup

Connecting to web server via FTP

Connection details have been emailed to you.

In lab, I will demonstrate how to use WinSCP to connect to our remote server.

Here are the server details:


1. Creating a basic form process by a PHP script

  1. Open Notepad++ or your editor of choice
  2. Create a new document and save it as part1.html in your lab3 folder
  3. Type the following starter code:
    p1
  4. Create another new document and save it as part1.php in your lab3 folder
  5. Type the following starter code:
    p1
  6. Using WinSCP, upload your lab3 folder to your remote server
  7. Open Chrome and go to your URL, i.e., username.siencs.com or username.breimer.net to test your scripts.

Key Concept: please read as you will be quizzed on this later

HTML has elements for building forms to create user interfaces. When a form is submitted, the browser can initiate an HTTP request based on the form's action attribute, which can specify the name or URL of a PHP server-side script. The values of form elements are passed to the server as part of the HTTP requests. PHP scripts can access these values using special global variables called $_GET and $_POST.

Task Check

Show your instructor part1.html working on the web server.


2. Adding a dropdown menu and some formatting

  1. Modify part1.html by replacing the text box for entering the number of nights with a drop down menu, i.e., select and option elements. The menu should let the user select the values 1, 2, 3, ..., 9. See the Mozilla reference for the select tag
  2. Add another drop down menu for selecting the number of rooms. The menu should let the user select the values 1, 2, 3, ..., 5. Give the select element the name rooms.
  3. Modify part1.php to "get" the number of rooms and store it as a PHP variable called $rooms. Then modify the output of the sentence:
    X rooms and Y nights at Z/night is a total cost of S
    Where X, Y and Z come from the form and S is computed by the PHP script.
  4. Modify the output so that the cost of the room, i.e., Z/night is in a strong tag.
  5. Add inline CSS to the strong tag so the color is red.
  6. Using WinSCP, upload your modified part1.html and part1.php
  7. Open Chrome and go to your URL, i.e., username.siencs.com or username.breimer.net to test your scripts.

Key Concept: please read as you will be quizzed on this later

How Form Submission Works:

Task Check

Show your instructor part1.html working on the web server.

3. Adding a radio button group and changing the submission method

  1. Modify part1.html by adding two labels and two radio buttons so the user can select either a smoking or non-smoking room. The two radio buttons should both have the name "type" with the values "smoking" and "non-smoking" respectively. See the Mozilla reference for radio buttons
  2. Change the form's method from "get" to "post"
  3. Modify part1.php by using the $_POST variable instead of the $_GET variable to fetch the form data.
  4. Modify part1.php to fetch the room type as a PHP variable called $type. Then modify the output of the sentence:
    X T rooms and Y nights at Z/night is a total cost of S
    Where T is the value of the room type, i.e., "smoking" or "nonsmoking" e.g., 3 smoking rooms and 4 nights at 100/night is a total cost of 1200.
  5. Add dollar signs so the output would now read something like "2 non-smoking rooms and 3 nights at $150/night is a total cost of $900"
  6. Using WinSCP, upload your modified part1.html and part1.php
  7. Open Chrome and go to your URL, i.e., username.siencs.com or username.breimer.net to test your scripts.

Key Concept: please read as you will be quizzed on this later

Key Concept: Receiving data via the GET method is very useful because you can see the submitted values in the URL which is helpful for debugging. In a URL, a question mark ? is used to define a query string. The query string consists of name=value pairs. Each pair is delimited with a &. Passing data via the POST is more common because it hides the submitted values and it allows large amounts of data to be passed. The size of the URL is limited so you cannot pass large amounts of data using GET.

Task Check

Show your instructor the part1.html form working on the web server.

DELIVERABLE

Create a zip file of your lab3 folder called lab3.zip and submit the file in Blackboard. The zip file should contain three files: part1.html and part1.php.

If applicable, in the comment area of Blackboard put your partner's name.

Not finished?

The deliverable must be submitted in Blackboard by midnight on the day before your next scheduled lab meeting. If you wish to work alone, you can submit the deliverable yourself. However, you are encouraged to work with your partner and you can submit together. When submitting as a lab pair, be sure to include your partner's name in the comment area when you submit via Blackboard.

Do not share

Outside of lab, you are only permitted to work with your lab partner. Do not share your work with any other student.