Creating Bulletin Boards
Creating a simple CGI message board
The HTML Templates
The Bulletin Board Script
Displaying the Message List
Displaying Messages
Adding New Messages
Adding Replies
Expiring Messages
The Complete Bulletin Board Script


[Previous] [Next]


Adding Replies


In this section, you develop the other subroutine needed for posting messages to your bulletin board. The Add_New_Message subroutine you developed in the previous section is used only for adding new messages to the bulletin board, not for replying to existing messages. For this, you develop the Add_Reply subroutine. The Add_Reply subroutine is very similar to the Add_New_Message subroutine. The only difference is that it does not create a new list file. Instead, the header for the new reply is added to the list file containing the header information for the message the reply references.

The lines for retrieving, incrementing, and saving the new message count file name as well as the lines for creating the new message file are exactly the same as those in the Add_New_Message subroutine. However, the addition of the header information to the list file is different. The following lines of Perl code open the existing list file and append the new header information to it:

 open(LIST,">>$list_dir/$data{\"list\"}") || die "Content-type:
 text/html\n\nCannot open list!";
 print LIST "$data{\"name\"}::$data{\"subject\"}::${date}::$num_messages\n";
 close(LIST);


Notice that the file name in the open statement is preceded by the >> operator. This operator indicates that the file will be opened for output and the output will be appended to the file rather than overwriting it.

Listing 6 contains the complete Add_Reply subroutine. As with the Add_New_Message subroutine, the Name, Subject, and Comments fields are checked for values, and the subroutine exists if they do not contain any. Also, the Display_Message_Lists subroutine is called after the user's reply has been posted to the bulletin board.

Listing 6: The Add_Reply Subroutine
sub Add_Reply {
   local (%data) = @_;
   local ($num_lists, $num_messages);

   # Verify the user entered the required fields
   die "Content-type: text/html\n\nYou must enter data for every field
 except the E-mail address."
         unless ($data{'name'} && $data{'subject'} &&
 $data{'comments'});

   # Get the last message number
   open(MESSAGES,"$message_count") || die "Content-type:
 text/html\n\nCannot open message count!";
   $num_messages = <MESSAGES>;
   close(MESSAGES);

   # Increment the number
   $num_messages++;

   # Save the current message number to the file
   open(MESSAGES,">$message_count") || die "Content-type:
 text/html\n\nCannot open message count!";
   print MESSAGES $num_messages;
   close(MESSAGES);

   # Create the new message
   # Windows users need to change the string
 ">$message_dir/$num_messages" to
   # ">$message_dir\\$num_messages"
   open(NEWMESSAGE,">$message_dir/$num_messages") || die "Content-type:
 text/html\n\nCannot create new message!";
   print NEWMESSAGE "<B>Subject:</B> $data{\"subject\"}<BR>\n";
   print NEWMESSAGE "<B>From:</B> $data{\"name\"}<BR>\n";
   print NEWMESSAGE "<B>E-mail:</B> $data{\"email\"}<BR>\n" if
 $data{'email'};
   print NEWMESSAGE "<B>Date:</B> $date<P>\n";
   print NEWMESSAGE "$data{\"comments\"}<P>\n";
   close(NEWLIST);

   # Add message header to the list
   # Windows users need to change the string
 ">>$list_dir/$data{\"list\"}"
   # to ">>$list_dir\\$data{\"list\"}"
   open(LIST,">>$list_dir/$data{\"list\"}") || die "Content-type:
 text/html\n\nCannot open list!";
   print LIST
 "$data{\"name\"}::$data{\"subject\"}::${date}::$num_messages\n";
   close(LIST);

   &Display_Message_Lists;

 }






[Previous] [Next]