Article
Programming Perl 101
Welcome to Part two of this Perl Tutorial. In Part one, you were shown how a Perl script can take information submitted via an HTML and display the results back to the person submitting the form by making use of variables. Variables are the core of the Perl language, so today we're going to learn a bit more about them.
1) Test.html
Create another HTML page and name it "test.html." Put the following code onto the page:
<html>
<head>
<title>Test.html</title>
</head>
<body>
<form action="http://www.yourdomain.com/cgi-bin/test.cgi" method="post">
<input type="hidden" name="action" value="1">
<input type="submit" value="Page 1">
</form>
<br><hr><br>
<form action="test.cgi" method="post">
<input type="hidden" name="action" value="2">
<input type="submit" value="Page 2">
</form>
<br><hr><br>
<form action="test.cgi" method="post">
<input type="hidden" name="action" value="3">
<input type="submit" value="Page 3">
</form>
</body>
</html>
Don't forget to substitute the form action URL with the path to your test.cgi file.
Notice the forms…they have only a submit button, and a "hidden" form field. This form field is obviously invisible to the visitor, however it can still hold a variable. If we click on the first button (labeled "Page 1"), it well pass the data along to the test.cgi file. Which data, you ask? Why, the data that says that the "action" field has a value of "2" of course! Using these values, we can tell the script to determine which button was pressed, and display the corresponding text and/or HTML.
Obviously the next step is to create the test.cgi file…let's do that now.
2) Test.cgi
Create a text file and save it as "test.cgi"…as usual, you'll need the subparseform.lib file uploaded to the same directory as test.cgi. As before, if you cannot find this through a web search, email me (chris@movieforums.com), and I'll send it to you as an attachment.
On with things; enter this into test.cgi:
#!/usr/local/bin/perl
require "subparseform.lib";
&Parse_Form;
$action = $formdata{'action'};
As you can see, we have the standard shebang line first. Secondly, we have a command that makes sure that subparseform.lib is in the correct directory. Third, we have a subroutine (don't worry about that word for now) that is being called to help us process the data being send from test.html, and fourth, we have a line that takes the value of the action field from test.html, and assigns it the variable "$action."