Building a Web Form - A Script to Process It
IMPORTANT: These instructions are for the www.indstate.edu server. How do Web Forms Work?Form content is processed by a special program written for that purpose. The web server uses the common gateway interface to run that program.
CGI (common gateway interface) refers to the convention by which the web serving process initiates another executable program on the machine. All scripts are executable programs; not all programs are scripts. Using agreed-upon conventions, the web serving process passes information to and receives information from the program. This program is called a cgi script (or program) because it is the object of the cgi interface.
The ToolsSSHSSH will allow you to login to a remote computer and issue commands to get that remote computer to do certain things, such as edit or copy a file. Official ISU SSH documentation is available here: Common UNIX Commands: ls display list of files cat [filename] display contents of file more [filename] display file with pauses pico [filename] edit a file cp [filename1] [filename2] copy file mv [filename1] [filename2] rename (move) file rm [filename] delete (remove) file rmdir [dirname] delete (remove) directory mkdir [dirname] create (make) directory cd [dirname] change working directory man [command] get help PicoPico is a text editor. This editor will allow you to edit and create text files right on the web server. After you have logged into your account, you can run Pico by typing pico at the Unix prompt and pressing enter. This will create a new file for you to edit. To open an existing file, type pico filename and press enter. Pico is fairly simple to use. Use the arrow keys to move through your document. Commands for using Pico are listed at the bottom of the screen. Most of the commands are a combination of the control key and a letter. For instance, to move down a screen in your document press control -Y. After you are finished editing your document, you will want to exit and save it. To exit and save your document:
Web BrowserYou will also need a web browser such as Netscape to view and test your forms. The Nuts and Bolts of FormsFor each form item, whether it be a checkbox, a text entry field, or a pull down list, you must define the following: Input Type: this determines how the form item will look. Will it be a check box, a fill in the blank, or a pull down list? Name: This is what you are going to call this particular form item. It is similar to a field name. The name of a form item is how you pull information from that form with your cgi script. Avoid using spaces in the names of your fields. Use underscores instead of spaces. For instance, instead of using "favorite color" as a name, you should use "favorite_color". Values: This is what is in that field. For instance, you may have a form element whose name is "type of car" and the Values a user can pick for the "type of car" field are "Ford", "Chevrolet", and "Toyota." As with names, avoid using spaces in the values of a field. The syntax for defining Input Type, Name, and Value for each form input type is different. Form Input Items<form></form> tagsThis tag tells the web browser that anything lying between the <form></form> tags should be considered an HTML form. This tag is necessary for writing an HTML form. For each <form> tag you must define the Method and the Action.
A sample <form> tag here on a document residing on web.indstate.edu would look like this: <form METHOD="POST" ACTION="mailscript.cgi"> form tags here </form> Plain Text Entry <input type="text">The code for a plain text entry form item would look like this: <input type="text" Name="favorite_animal" Value="frog" size="30">
A plain text entry form element looks like this: Please enter the name of your favorite animal: Text Box <textarea></textarea>The code for a text box form item would look like this: <TEXTAREA Name="description" Rows="4" Cols="40"> You can leave this line blank or you can add text to have it appear in the text box. </TEXTAREA>
A text box form element looks like this: Please describe your pet: Select Menu <select></select>The code for a select menu would look like this: <select name="favorite_pet"> <option value="cat"> Cat <option value="frog"> Tree frog <option value="dog">Dog </select>
A select menu form element looks like this: Please select your favorite pet from the list: Checkboxes <input type="checkbox">The code for check boxes look like this: <input type="checkbox" name="dog" value="yes_I_have_a_dog"> Yes, I have a dog. <input type="checkbox" name="cat" value="yes_I_have_a_cat">Yes, I have a cat <input type="checkbox" name="frog" value="yes_I_have_a_frog"> Yes, I have a frog.
Check box form elements look like this: Please check all that apply: Radio Buttons <input type="radio">The code for radio buttons look like this: <input type="radio" name="frog_type" value="whites">Whites Tree Frog <input type="radio" name="frog_type" value="green_mantella">Green Mantella <input type="radio" name="frog_type" value="barking" CHECKED>Barking Tree Frog <input type="radio" name="frog_type" value="red_legged">Red Legged Walking Frog
Radio button form elements look like this: Which type of frog do you own? Reset and Submit ButtonsThe code for reset and submit buttons look like this: <INPUT TYPE="reset" VALUE="Clear fields"> <INPUT TYPE="submit"> Reset and submit button form elements look like this: Script ElementsIT has provided a simple mailing script that you can customize. Make sure you save the file as text (not source) or cut and paste the information from the web browser into your text editor. If you have an account on web.indstate.edu you can also copy the file directly into your account by issuing the following command from the SSH prompt: cp /it/user-serv/websupport/form-script.cgi my-script.cgi Once you have copied the sample script into your web account (make sure you it has a .cgi extension) , you will need to customize the items below so that it will work with the form you have developed. To customize the form, you must be familiar with a few basic elements of the Perl scripting language: # comment line. This line is not processed by the computer. It is for the script writer's reference $Form{'name'} this calls the value from the form in the specified field. Using one of the examples above, $Form{'type_of_frog'} in which the person had selected "Whites Tree Frog" would be equal to "whites," the value of the field "type_of_frog" in the current form. If the script instructed the computer to print $Form{type_of_frog} it would print "whites" in this case. ; this specifies the end of a line of code. Each line of code should end with a ; \n is the symbol for a line return. Use \n in your print statements if you don't want the email message your script generates to be all on one line. \ use the backslash before any special characters such as the @ sign in an email address. Configuration Options:#---------------------------- # Start Configuration Options #---------------------------- # Edit this line to specify one or more domain names authorized to use this # service (can be full or partial domain name) @Referers = ( "web.indstate.edu", "www-isu.indstate.edu" ); # Edit this line to specify the email address of the recipient that should # receive this evaluation $Recipient = "dduck@water.indstate.edu"; # Edit this line to specify the full path to your send Internet mail utility $MailUtil = '/usr/lib/sendmail'; # Edit this line to specify the title of the resultant HTML response $Title = 'Pet Information'; # Edit this line to specify the heading of the resultant HTML response $Heading = 'Pet Information'; #-------------------------- # End Configuration Options #-------------------------- Adjusting what the response page will look like# Print a title and initial heading print "<HEAD><TITLE>$Title</TITLE></HEAD>" print "<BODY bgcolor=white>" print "<img src='http://web.indstate.edu/it/user-serv/graphics/header.gif'>" print "<H1>$Heading</H1>" Making Form Fields Required# Send blank form error message if no comments were specified
# &blank_response unless $FORM{'favorite_animal'};
# &blank_response unless $FORM{'pets'};
# &blank_response unless $FORM{'name'};
# &blank_response unless $FORM{'email'};
AND sub blank_response
{
print "Your comments appear to be blank and thus were not sent to the";
print " specified recipient.\n";
exit( 0 );
}
Sending the Mail Message and Response Screen.# Now send mail to specified recipient
open ( MAIL, "|$MailUtil -t -n -oi" ) || die "Unable to open $MailUtil\n";
print MAIL "To: $Recipient\n";
print MAIL "From: webmaster\@indstate.edu (ISU Web Server)\n";
print MAIL "Reply-to: $FORM{'email'} ($FORM{'name'})\n";
print MAIL "Subject: Animal Query (Forms Submission)\n\n";
print MAIL " \n";
print MAIL "What is your favorite animal: $FORM{'favorite_animal'}\n";
print MAIL "\n";
print MAIL "Do you have any pets? $FORM{'pets'}\n";
print MAIL "\n";
print MAIL "What is the name of your pet? $FORM{'name_of_pet'}\n";
print MAIL "\n";
print MAIL "Name: $FORM{'name'}\n";
print MAIL "\n";
print MAIL "Email: $FORM{'email'}\n";
print MAIL " \n";
close ( MAIL );
AND # Make the person feel good about submitting these comments< print "Thank you for filling out this form. "; Calling the Script from your FormAfter you have customized your script and put it into your account on the web server, you need to edit your html form to call that specific script. Calling the script is done from the <form> tag. To call a script with the filename of favorites.cgi, your <form> tag should look like this: <form method="POST" ACTION="favorites.cgi"> form data here </form> In order for your script to run, you must make sure that it is executable. To do this, from the SSH prompt type: chmod 755 scriptfilename.cgi and press enter. For instance, to make sure the script favorites.cgi is executable you would type: chmod 755 favorites.cgi and press the enter key. TroubleshootingOften your script won't work the first time you run it. Here are some troubleshooting hints:
|
||
This page is maintained by web@indstate.edu |