Week 3
December 4, 2007 – 12:15 amSession 1: Some important pre-processing is required for text areas within forms, otherwise every time a user hits enter in a text field the flat file will contain the “return” and “new line” characters. This means our flat file will essentially be corrupt because the “new line” character is used to distinguished between one record and the next. The following function replaces the character that corrupts the flat file with an HTML break tag:
$comments = str_replace("\r\n","",$comments);
It’s easy enough to display the file line by line, but what if you want to have each field within the lines line up within an HTML table and include other formatting options, such as bolding, or a “mailto” link?
In order to do this you need to use the “explode” function to extract the individual fields into an array which we can then display in a dynamic web page. See the example below (show_guests.php):
<html>
<title>My Guests</title>
<head></head>
<body>
<table>
<tr><th>First Name</th><th>Last Name</th><th>Email</th></tr>
<?
$gb_file = file( 'guests.txt' );
foreach( $gb_file as $line ) {
rtrim( $line );
$field = explode( "\t", $line );
echo( "<tr>\n" );
echo( "<td>" . $field[0] . "</td>\n" );
echo( "<td>" . $field[1] . "</td>\n" );
echo( "<td><a href=\"mailto:$field[2]\">$field[2]</a></td>" );
echo( "</tr>" );
}
?>
</table>
</body>
</html>
Complete the guestbook project by adding your own personal design touch to the elements of the project, including the form (guestbook.html), the action (guestbook.php) and the show_guests.php script. Here’s a list of requirements for the project and how you’re expected to turn in the project:
- The guest book form (guestbook.html) must have at least 10 fields for the user to input.
- User input must contain arrays by using checkboxes or a select multiple list.
- Use loops to gather information from the arrays.
- The form action (guestbook.php) must send an email and write the fields to the “guests.txt” file.
- The form action must strip harmful header injection attacks.
- The guestbook.php script should provide feedback including thanking the user after they submit the form.
- The show_guests.php script should display all the guests in a table lined up evenly.
- Copy all your code to the drop off drive and a link to your project on OLS.
- This project is due Session 1, Week 4.
Session 2: We talked about the dynamic links page project.To begin the links project create a directory called “links” inside your “php” directory. Inside the “links” fold create a form like the one below. The form should have a text field for the url, the category and the description.
To see a working model of the links project click here.
Download: PHP Problem Set 3
1. All PHP problems within a “set” are due by the beginning of class on the next day that the class meets.
2. Answers must be available via links on the OLS or comparable hosting and copied to the drop off drive.
3. Solutions to each problem set will be given during demonstrations on the day that they are due.
4. Grades will be collected during the solution demonstrations by an assigned classmate.