You are here

Using PHP For Interactive Web Pages - A Simple Example

Author(s): 
Andrew G. Bennett

 

My first example is an easy web form. While standard HTML provides many elements to make it easy to produce good looking forms for a web site, it has traditionally required some CGI programming to do anything with the data submitted. With PHP, it is easy to create a page that accepts the data and either e-mails it to the instructor, saves it to a database, or takes some other action depending upon what is submitted.

For this example, I've put together a reaction form for students to describe a section in their calculus textbook. I have found it useful to ask students to read the section in the text before lecture and turn in a note card at the beginning of lecture with either a short summary of the section or a question they wished to have answered (the "muddiest point" in the section). I distribute the cards to recitation teachers so they have a sense of where the students in their recitations are having difficulties. It would be helpful to have the material available in time to respond to it in lecture directly, but since few students can profitably identify difficult areas more than one section ahead of the lecture, I can't realistically collect cards before lecture. I can let them e-mail their responses, however, and I read selected e-mail an hour before lecture. My experience with other assignments is that I get much better feedback if I provide more structure by having the student fill in an online form rather than just write an e-mail -- even if I carefully indicate the specific questions and the structure to use in writing the e-mail. A more complete discussion of the use and advantages of online preparatory assignments, though from the point of view of teaching Physics, can be found in Novak et. al. (1999).

The web form is written in one page. The action element of the form tag directs the input to a second page that packages the input into an e-mail message to the instructor and prints a short confirmation for the student. The Sample Form in the links is live, so you can test out the interaction before (or after) looking at the source code. I've also included a link to a typical response as it arrives in my email. (Links will open in a new window.)

Note that jittform.html is a standard HTML form, without any PHP code at all. The response page, jittresp.php, does include PHP code, but that won't show up if you look at the page source in your browser, as your browser has received only the output of the code from the server. (The full source is listed on the last link above.) In PHP, variables do not have to be declared, since they follow a standard syntax of a $ followed by a variable name (with the usual limitations of no spaces, alphanumeric, etc.). Any data posted by a form are automatically placed in a variable with the same name as the corresponding form element. So $query will hold the input from the textarea with the name query on the form and so forth. Note that branching statements in PHP affect the plain HTML located between the {} of the PHP code, even though the HTML code doesn't lie inside tags. So this code from jittresp.php


<?php
$msg="Name:
$name

Email:
$email

Summary:
$summary

Problem:
$query";
if (mail("bennet@math.ksu.edu" , "Section 3.3 - $name" , $msg)) {
?>
Your preparatory material has been mailed to the instructor. Thank you.
<?php } else { ?>
The system was unable to mail your material. Please press the back button
on your browser and try again. If the problem persists, please contact the
instructor via email at
<href=mailto:bennett@math.ksu.edu>bennett@math.ksu.edu</a>
<?php } ?>

will produce either the confirmation that the mail was sent or the error message depending on whether the mail function returns true or false.

This example is designed to show how little PHP code is needed for a useful page. Since PHP is a full language, it would be possible -- not particularly difficult -- to provide for a number of additional features, such as

  • including the student responses on the confirmation page,
  • allowing students to check their work and choose whether to edit or submit their answers, or
  • logging responses to a database rather than sending them via e-mail.

Warning: I recommend that you configure your e-mail client to direct e-mail with subject "Section 3.3" to a separate inbox if you use this form in a large lecture.

Andrew G. Bennett, "Using PHP For Interactive Web Pages - A Simple Example," Loci (December 2004)

JOMA

Journal of Online Mathematics and its Applications

Dummy View - NOT TO BE DELETED