Creating the HTML Form

Among the things Lincoln explains in his documentation is how you can use CGI.pm not only to process the output of HTML forms, but to actually produce those forms in the first place. With this approach, you just send a user to your CGI script, and the first time the user invokes the script it delivers an HTML form with all the values set to their defaults. Then, when the form is submitted back to the same script, it takes the data supplied by the user and does whatever you want it to do.

CGI.pm has lots of nifty features like that, but they tend to be a bit overwhelming for beginners, so in this case we’re going to take a more straightforward approach and simply create our form as a standard HTML page, then submit it to our CGI script for processing.

Example 3-1 shows an HTML form you can use for this demonstration. I’m not going to bother explaining what’s going on with the table tags and form elements in this web page; again, I’m assuming you already know about those things, or can learn about them elsewhere.

You can download your own copy of this web page from this book’s online example repository, at http://www.elanus.net/book/.Or you can just create your own copy of it.

Example 3-1. A page with an HTML form for testing a CGI form-to-email gateway

<HTML> <HEAD> <TITLE>Sample Form</TITLE> </HEAD> <BODY> <H1>Sample Form</H1> <P>Please fill out this form and submit it. Thank you.</P> <FORM ACTION="mail_form.cgi" METHOD="POST"> <TABLE> <TR> <TD ALIGN="right"><STRONG>My ...

Get Perl for Web Site Management now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.