Exercises: Saving form data

Exercise: Email light bulb joke

Write a PHP program that emails a light bulb joke. Here is the form:

Form

Figure 1. Form

When the user clicks the button, some PHP sends an email somewhere, and then shows this:

Output

Figure 2. Output

Only use your own email address! Don’t spam.

You can try my version (it doesn’t actually send email, just shows the output). You can download the files.

Upload your solution to your server. Put the URL below.

(Log in to enter your solution to this exercise.)

Exercise: Joke file

Create an application to save jokes to a file. The main menu is like this:

Main page

Figure 1. Main page

Click “See joke list” and you see the jokes file:

Show jokes

Figure 2. Show jokes

The user can also type a new joke into the main page:

Adding a joke

Figure 3. Adding a joke

Clicking the button will add the joke to the file, and show a confirmation page:

Joke added

Figure 4. Joke added

You can try my solution, though it doesn’t actually save new jokes. You can also download the files.

Upload your solution to your server. Put the URL of the main page below.

(Log in to enter your solution to this exercise.)

Exercise: Quote collector

Write a PHP application that lets you collect quotes you like. It should let you:

  • See quotes you have collected.
  • Write quotes to a file.

You can try the application.

The page that shows the quotes should say “No quotes yet” if there are none. Otherwise, it should show the quotes.

Separate quotes with <hr> tags.

save-quotes.php is the file that saves a new quote. It should:

  • Use stripslashes() to remove PHP’s “helpful” backslashes.
  • Replace new lines with <br> tags.

These two lines will help:

$quote = stripslashes($_POST['quote']);
$quote = str_replace("\n", '<br>', $quote);

You can download a zip file that has everything except two PHP files: see-quotes.php and save-quotes.php.

You can also download my solution, but do the exercise yourself first!

Put the URL of your solution below.

(Log in to enter your solution to this exercise.)

Exercise: Ticket booth

A zoo has a ticket booth. Visitors come up to the booth, and order tickets for adult and children (e.g., 1 adult and two children). The booth attendant collects the entry fee, and prints out the tickets.

Write a PHP application for the attendant. It starts out like this:

Empty form

Figure 1. Empty form

Notice that the focus is in the first input field.

You can download the background image. It’s from a design on OSWD, by GGGDesign.

The attendant enter the numbers of adults and children, and clicks the Total button. The page shows the total, and a Print button:

Form with data

Figure 2. Form with data

When the attendant clicks the Print button, three things happen.

  • A small printer prints the tickets (OK, just pretend this happens).
  • A record of the transaction is appended to a log file.
  • The blank form (Figure 1) is shown again.

Clicking on the “Show log” link shows the transaction log:

Log

Figure 3. Log

Each log entry has the date and time, as well as the number of tickets ordered. This code might be useful:

date('l, F jS Y, h:i:s A')

You can try the application (although I have disabled the logging, to keep the file size down).

Upload your solution to your server. Put the URL below.

(Log in to enter your solution to this exercise.)


How to...

Lessons

User login

Log in problems? Try here


Dogs