📄 ch20.htm
字号:
</BLOCKQUOTE><P>This HTML line specifies an input field with a default value of<TT>WasWaldo</TT>. The input box willbe 25 characters long although the user can enter up to 50 characters.<P>At times, you may want the user to be able to enter text withoutthat text being readable. For example, passwords need to be protectedso that people passing behind the user can't secretly steal them.In order to create a protected field, use the <TT>password</TT>type.<BLOCKQUOTE><PRE><INPUT TYPE=password NAME=password SIZE=10><BR></PRE></BLOCKQUOTE><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD><B>Caution</B></TD></TR><TR><TD><BLOCKQUOTE>The <TT>password</TT> input option still sends the text through the Internet without any eNCryption. In other words, the data is still sent as clear text. The sole fuNCtion of the <TT>password</TT> input option is to ensure that the password is not visible on the screen at the time of entry.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><P>The <TT><INPUT></TT> tag isalso used to define two possible buttons-the submit and resetbuttons. The submit button sends the form data to a specifiedURL-in other words to a CGI program. The reset button restoresthe input fields on the forms to their default states. Any informationthat the user had entered is lost. Frequently, the <TT>VALUE</TT>modifier is used to change the text that appears on the buttons.For example:<BLOCKQUOTE><PRE><INPUT TYPE=submit VALUE="Process Information"></PRE></BLOCKQUOTE><P>Hidden fields are frequently used as sneaky ways to pass informationinto a CGI program. Even though the fields are hidden, the fieldname and value are still sent to the CGI program when the submitbutton is clicked. For example, if your script generated an emailform, you might iNClude a list of email addresses that will becarbon-copied when the message is sent. SiNCe the form user doesn'tneed to see the list, the field can be hidden. When the submitbutton is clicked, the hidden fields are still sent to the CGIprogram along with the rest of the form information.<P>The last two input types are checkboxes and radio buttons. Checkboxeslet the user indicate either of two responses. Either the boxon the form is checked or it is not. The meaning behind the checkboxdepends entirely on the text that you place adjacent to it. Checkboxesare used when users can check off as many items as they'd like.For example:<BLOCKQUOTE><PRE><INPUT TYPE=checkbox NAME=orange CHECKED>Do you like the color Orange?<INPUT TYPE=checkbox NAME=blue CHECKED>Do you like the color Blue?</PRE></BLOCKQUOTE><P>Radio buttons force the user to select only one of a list of options.Using radio buttons for a large number of items (say, over five)is not recommended because they take up too much room on a webpage. The <TT><SELECT></TT>tag should be used instead. Each grouping of radio buttons musthave the same name but different values. For example,<BLOCKQUOTE><PRE>Operating System:<BR><INPUT TYPE=radio NAME=os VALUE=Win95>Windows 95<INPUT TYPE=radio NAME=os VALUE=WinNT>Windows NT<INPUT TYPE=radio NAME=os VALUE=UNIX CHECKED>UNIX<INPUT TYPE=radio NAME=os VALUE=OS2>OS/2CPU Type:<BR><INPUT TYPE=radio NAME=cpu VALUE=Pentium>Intel Pentium<INPUT TYPE=radio NAME=cpu VALUE=Alpha CHECKED>DEC Alpha<INPUT TYPE=radio NAME=cpu VALUE=Unknown>Unknown</PRE></BLOCKQUOTE><P>You should always provide a default value for radio buttons becauseit is assumed that one of them must be selected. Quite ofen, itis appropriate to provide a "none" or "unknown"radio button (like the "CPU Type" in the above example)so that the user won't be forced to pick an item at random.<P>Another useful form element is the drop-down list input fieldspecified by the <TT><SELECT>..</SELECT></TT>set of tags. This form element provides a compact way to let theuser choose one item from a list. The options are placed insidethe <TT><SELECT>..</SELECT></TT>tags. For example,<BLOCKQUOTE><PRE><SELECT NAME=weekday><OPTION SELECTED>Monday<OPTION>Tuesday<OPTION>Wednesday<OPTION>Thursday<OPTION>Friday</SELECT></PRE></BLOCKQUOTE><P>You can use the <TT>SELECTED</TT>modifier to make one of the options the default. Drop-down listsare very useful when you have three or more options to choosefrom. If you have less, consider using radio buttons. The <TT><SELECT></TT>tag has additional options that provide you with much flexibility.You can read about these advaNCed options at:<BLOCKQUOTE><PRE>http://robot0.ge.uiuc.edu/~carlosp/cs317/ft.4-5f.html</PRE></BLOCKQUOTE><P>The last form element that I should mention is the text box. Youcan create a multi-line input field or text box using the <TT><TEXTAREA>..</TEXTAREA></TT>set of tags. The <TT><TEXTAREA></TT>tag requires both a <TT>ROWS</TT>and a <TT>COLS</TT> modifer. You canplace any default text for the text box inside the <TT><TEXTAREA>..</TEXTAREA></TT>tags.<BLOCKQUOTE><PRE><TEXTAREA NAME=comments ROWS=3 COLS=60></TEXTAREA></PRE></BLOCKQUOTE><P>The user's Web browser will automatically provide scroll barsas needed. However, the text box will probably not word-wrap.In order to move to the next line, the user must press the enterkey.<BR><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD><B>Note</B></TD></TR><TR><TD><BLOCKQUOTE>If you'd like a more advaNCed introduction to HTML forms, try this web site:</BLOCKQUOTE><BLOCKQUOTE><TT>http://robot0.ge.uiuc.edu/~carlosp/cs317/ft.1.html</TT></BLOCKQUOTE></TD></TR></TABLE></CENTER><P><H2><A NAME="HandlingFormInformation"><FONT SIZE=5 COLOR=#FF0000>Handling Form Information</FONT></A></H2><P>There are two ways for your form to receive form information-the<TT>GET</TT> method and the <TT>POST</TT>method. The transfer mechanism is specified in the <TT><FORM></TT>tag using the <TT>METHOD</TT> modifier.For example, the following HTML line tells the client web browserto send the form information back to the server using the <TT>GET</TT>method.<BLOCKQUOTE><PRE><FORM METHOD=get ACTION=/cgi-bin/gestbook.pl></PRE></BLOCKQUOTE><P>The <TT>GET</TT> method appends allof the form data to the end of the URL used to invoke the CGIscript. A question mark is used to separate the original URL (specifiedby the <TT>ACTION</TT> modifier inthe <TT><FORM></TT> tag) andthe form information. The server software then puts this informationinto the <TT>QUERY_STRING</TT> environmentvariable for use in the CGI script that will process the form.<P>The <TT>GET</TT> method can't be usedfor larger forms because some web servers limit the length ofthe URL portion of a request. (Check the documentation on yourparticular server.) This means that larger forms might blow upif submitted using the <TT>GET</TT>method. For larger forms, the <TT>POST</TT>method is the answer.<P>The <TT>POST</TT> method sends allof the form information to the CGI program using the <TT>STDIN</TT>filehandle. The web server will set the <TT>CONTENT_LENGTH</TT>environment variable to indicate how much data the CGI programneeds to read.<P>The rest of this section develops a fuNCtion capable of readingboth types of form information. The goal of the fuNCtion is tocreate a hash that has one entry for each input field on the form.<P>The first step is simply to read the form information. The methodused to send the information is stored in the <TT>REQUEST_METHOD</TT>environment variable. Therefore, we can examine it to tell ifthe fuNCtion needs to look at the <TT>QUERY_STRING</TT>environment variable or the <TT>STDIN</TT>filehandle. Listing 20.1 contains a fuNCtion called <TT>getFormData()</TT>that places the form information in a variable called <TT>$buffer</TT>regardless of the method used to transmit the information.<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>Define the </I><TT><I>getFormData()</I></TT><I>fuNCtion.<BR>Initialize a buffer.<BR>If the </I><TT><I>GET</I></TT><I>method is used, copy the form information into the buffer.<BR>If the </I><TT><I>POST</I></TT><I>method is used, read the form information into the buffer.</I></BLOCKQUOTE><HR><BLOCKQUOTE><B>Listing 20.1 20LST01.PL-The First Step Is to Getthe Form Information<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>sub getFormData { my($buffer) = ""; if ($ENV{'REQUEST_METHOD'} eq 'GET') { $buffer = $ENV{'QUERY_STRING'}; } else { read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); }}</PRE></BLOCKQUOTE><HR><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD><B>Tip</B></TD></TR><TR><TD><BLOCKQUOTE>SiNCe a single fuNCtion can handle both the <TT>GET</TT> and <TT>POST</TT> methods, you really don't have to worry about which one to use. However, because of the limitation regarding URL length, I suggest that you stick with the <TT>POST</TT> method.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><P>I'm sure that you find this fuNCtion pretty simple. But you mightbe wondering what information is contained in the <TT>$buffer</TT>variable.<P>Form information is passed to a CGI program in <TT>name=value</TT>format and each input field is delimited by an ampersand (<TT>&</TT>).For example, if you have a form with two fields-one called <TT>name</TT>and one called <TT>age</TT>-the forminformation would look like this:<BLOCKQUOTE><PRE>name=Rolf+D%27Barno&age=34</PRE></BLOCKQUOTE><P>Can you see the two input fields? First, split up the informationusing the <TT>&</TT> as the delimiter:<BLOCKQUOTE><PRE>name=Rolf+D%27Barnoage=34</PRE></BLOCKQUOTE><P>Next, split up the two input fields based on the <TT>=</TT>character:<BLOCKQUOTE><PRE>Field Name: name Field Value: Rolf+D%27BarnoField Name: age Field Value: 34</PRE></BLOCKQUOTE><P>Remember the section on URL eNCoding from <A HREF="ch19.htm" >Chapter 19</A>? You seeit in action in the name field. The name is really <TT>RolfD'Barno</TT>. However, with URL eNCoding spaces are convertedto plus signs and some characters are converted to their hexadecimalASCII equivalents. If you think about how a single quote mightbe mistaken for the beginning of an HTML value, you can understandwhy the ASCII equivalent is used.<P>Let's add some features to the <TT>getFormData()</TT>fuNCtion to split up the input fields and store them in a hashvariable. Listing 20.2 shows the new version of the <TT>getFormData()</TT>fuNCtion.<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>Declare a hash variable to hold the form's input fields.<BR>Call the </I><TT><I>getFormData()</I></TT><I>fuNCtion.<BR>Define the </I><TT><I>getFormData()</I></TT><I>fuNCtion.<BR>Declare a local variable to hold the refereNCe to the input fieldhash.<BR>Initialize a buffer.<BR>If the </I><TT><I>GET</I></TT><I>method is used, copy the form information into the buffer.<BR>If the </I><TT><I>POST</I></TT><I>method is used, read the form information into the buffer.<BR>Iterate over the array returned by the </I><TT><I>split()</I></TT><I>fuNCtion.<BR>Decode both the input field name and value.<BR>Create an entry in the input field hash variable.<BR>Define the </I><TT><I>decodeURL()</I></TT><I>fuNCtion.<BR>Get the eNCoded string from the parameter array.<BR>Translate all plus signs into spaces.<BR>Convert character coded as hexadecimal digits into regular characters.<BR>Return the decoded string.</I></BLOCKQUOTE><HR><BLOCKQUOTE><B>Listing 20.2 20LST02.PL-The First Step Is to Getthe Form Information<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>my(%frmFlds);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -