⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch10.htm

📁 美国Macmillan出版社编写的Perl教程《Perl CGI Web Pages for WINNT》
💻 HTM
📖 第 1 页 / 共 4 页
字号:
</TD></TR>

<TR VALIGN=TOP><TD WIDTH=132><CENTER>/ </CENTER></TD><TD WIDTH=114><CENTER>%2F</CENTER>

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=132><CENTER>_= </CENTER></TD><TD WIDTH=114><CENTER>%3D</CENTER>

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=132><CENTER># </CENTER></TD><TD WIDTH=114><CENTER>%23</CENTER>

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=132><CENTER>% </CENTER></TD><TD WIDTH=114><CENTER>%25</CENTER>

</TD></TR>

</TABLE></CENTER>

<P>

<P>

The specifics of MIME/URL encoding can be found in RFC 1552, section

3 at

<BLOCKQUOTE>

<PRE>

<A HREF="javascript:if(confirm('http://ds.internic.net/ds/dspg1intdoc.html  \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address.  \n\nDo you want to open it from the server?'))window.location='http://ds.internic.net/ds/dspg1intdoc.html'" tppabs="http://ds.internic.net/ds/dspg1intdoc.html">http://ds.internic.net/ds/dspg1intdoc.html

</A></PRE>

</BLOCKQUOTE>

<P>

The tag that uses the POST method might look like this:

<BLOCKQUOTE>

<PRE>

&lt;FORM Method=&quot;POST&quot; Action=&quot;http://www.my_server.com/cgi-bin/register.pl&quot;&gt;

</PRE>

</BLOCKQUOTE>

<P>

To collect specific information from the user, the &lt;INPUT&gt;

tag is used. The various attributes for this are found in Table

10.3.<BR>

<P>

<CENTER><B>Table 10.3 Form Tag Input Options</B></CENTER>

<P>

<CENTER>

<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>

<TR VALIGN=TOP><TD WIDTH=120><CENTER><B>Form Tag</B></CENTER></TD><TD WIDTH=456><B>Purpose</B>

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=120>Align</TD><TD WIDTH=456>Used when an image is employed in gathering the data. The choices are &quot;top,&quot; &quot;middle,&quot; and &quot;bottom,&quot; which define the relationship the image has to the text following it.

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=120>Checked</TD><TD WIDTH=456>Presets a checkbox to include a checkmark. If this attribute is not included, the checkbox will be blank.

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=120>Maxlength</TD><TD WIDTH=456>Sets the maximum number of characters a user can input as text into a field. The default is unlimited, so you might want to restrict the length in every form, or risk your scripts becoming 

overwhelmed by a flood of text.

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=120>Name</TD><TD WIDTH=456>The symbolic name used when transferring and identifying the output produced by your form.

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=120>Size</TD><TD WIDTH=456>Defines the field width of the text box presented to the user. When the Size is less than the Maxlength, then the text field will be scrollable.

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=120>Src</TD><TD WIDTH=456>If an image is used, this identifies the source of the image file.

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=120>Type</TD><TD WIDTH=456>The kind of input format the user sees is defined. The choices are checkbox (the user can make multiple choices for data values), hidden (the values are defined by the form, not by the user), and image 

(the user selects an area of the image, then the x and y coordinates are sent with the name/value pairs).

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=120>Password</TD><TD WIDTH=456>User supplies text that is hidden from view on the user's screen. Typically this appears as asterisks or dots.

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=120>Radio</TD><TD WIDTH=456>User must choose one selection only from a list. This should not be confused with checkboxes where the user can select any and all of the choices presented.

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=120>Reset</TD><TD WIDTH=456>Clears the form of all selections for re-entry by the user.

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=120>Submit</TD><TD WIDTH=456>Used by the user to submit the form data to the server. 

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=120>text</TD><TD WIDTH=456>Attribute that uses the Size and Maxlength provisions to create a single line field for user input. Text is a single line text input field. If this is the only area for user input, then a submit button is 

not required. Simply pressing the Enter or Return key on the user's keyboard will send the data on. If more than a single line is needed the &lt;TEXTAREA&gt; tag should be used.

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=120>Value</TD><TD WIDTH=456>Used with the radio button, this sets the value for the selection available to the user.

</TD></TR>

</TABLE></CENTER>

<P>

<P>

Using these tags we can create an in-depth form that asks for

user background data and tastes, which the CGI can then enter

into your databases. Some of these tags were used to gather information

in our guestbook script. After these tags are illustrated with

examples, they are combined to a full form page that can be adapted

to gather user data on your site.

<BLOCKQUOTE>

<PRE>

&lt;INPUT Type=&quot;hidden&quot; Name=&quot;address&quot; Value=&quot;new_user@my_server.com&quot;&gt;

     Your Name: &lt;INPUT Type=&quot;text&quot; Name=&quot;user-name&quot; Size=&quot;2&Oslash;&quot; Maxlength=&quot;3&Oslash;&quot;&gt;

&lt;H2&gt;Guess the secret word contest&lt;/H2&gt;&lt;BR&gt;

Try and guess our secret word to win a prize!&lt;INPUT Type=&quot;password&quot; Name=&quot;word_guess&quot;&gt;

Where did you hear about out site?:&lt;INPUT Type=&quot;radio&quot; Name=&quot;Internet&quot; Value=&quot;online&quot;&gt;The Internet

     Please check all the mediums you use:&lt;INPUT Type=&quot;checkbox&quot; Name=&quot;television&quot;&gt;

     Where are you from?&lt;INPUT Type=&quot;image&quot; Src=&quot;http://www.my_server.com/images/map.gif&quot; 

	 Name=&quot;user_location&quot; Align=&quot;top&quot;&gt;

     Do you like our site?:&lt;INPUT Type=&quot;radio&quot; Name=&quot;site_feedback&quot; Value=&quot;Yes&quot; Checked&gt;Yes

     &lt;INPUT Type=&quot;radio&quot; Name=&quot;site_feedback&quot; Value=&quot;No&quot;&gt;If not, then what don't you like?

&lt;INPUT Type=&quot;text&quot; Name=&quot;user_suggestions&quot; Size=&quot;60&quot; Maxlength=&quot;1&Oslash;&Oslash;&quot;&gt;

     &lt;INPUT Type=&quot;Submit&quot; Value=&quot;Send it in!&quot;&gt;

     &lt;INPUT Type=&quot;Reset&quot; Value=&quot;Do it again!&quot;&gt;

</PRE>

</BLOCKQUOTE>

<P>

To allow the user to choose from a list of options on a form,

the &lt;SELECT&gt; tag is used. Although the default for selection

is only one choice for the user, this can be modified using the

Multiple attribute. The &lt;OPTION&gt; tag is used to define each

choice available to the user. The attributes work as follows:

<UL>

<LI>Name-A name is assigned to be associated with the data submitted

by&nbsp;the user.

<LI>Multiple-Allows the user to make more than one choice in the

&lt;SELECT&gt; tag.

<LI>Size-Identifies how many choices will be available to the

user. If&nbsp;thi&nbsp;value is more than one, the choices are

presented as a list.

</UL>

<P>

The &lt;OPTION&gt; tag is used in tandem with the &lt;SELECT&gt;

tag. It has two attributes:

<UL>

<LI>Selected-Marks a choice as already selected.

<LI>Value-This is the value that is sent. If the &lt;SELECT&gt;

tag is selected, then this is the value that will be returned;

otherwise, the default is the value set by the &lt;OPTION&gt;

tag.

</UL>

<P>

Typically these two tags might look like this:

<BLOCKQUOTE>

<PRE>

Please choose one of our products as a gift:

     &lt;SELECT Name=&quot;product_gifts&quot;&gt;

&lt;OPTION&gt;Lead Pencil 2&Oslash;&Oslash;&Oslash;

&lt;OPTION&gt;Staple-O-Matic!

</PRE>

</BLOCKQUOTE>

<P>

The last tag available in creating forms is &lt;TEXTAREA&gt;,

which is used to define the size of a text field for user input.

This field is scrollable. The attributes that apply here are

<UL>

<LI>Name-Logical name linked to the data supplied here.

<LI>Rows-Number of rows in the field.

<LI>Cols-Number of columns in the field.

</UL>

<P>

This creates a better format for a text area in which to input

user feedback than the previous example, which created only a

text line. To change this, we can use a &lt;TEXTAREA&gt; tag that

looks like this:

<BLOCKQUOTE>

<PRE>

&lt;INPUT Type=&quot;radio&quot; Name=&quot;site_feedback&quot; Value=&quot;No&quot;&gt;If not, then what don't you like?

&lt;TEXTAREA Name=&quot;user_suggestions&quot; Rows=&quot;4&quot; Cols=&quot;5&Oslash;&quot;&gt;&lt;BR&gt;



     &lt;/textarea&gt;

</PRE>

</BLOCKQUOTE>

<P>

When you combine all these tags, you get a better understanding

of how the form works. This example combines the previous examples

in a form that presents a sample of each of the elements. It is

for collecting information about new users of your site. Screen

representations of what the form would look like is shown in Figure

10.1 right after the script.

<P>

<A HREF="f10-1.gif" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/f10-1.gif"><B>Figure 10.1 :</B> <I>Sample of different input features

on an HTML form</I>.</A>

<BLOCKQUOTE>

<PRE>

&lt;HTML&gt;

&lt;! - - Example of form elements and attributes - - &gt;

&lt;HEAD&gt;

&lt;TITLE&gt;

The New User Profile Form

&lt;/TITLE&gt;

&lt;/HEAD&gt;

&lt;BODY&gt;

&lt;P&gt;

We Want to Know More About You!&lt;BR&gt;

&lt;HR&gt;

&lt;BR&gt;

&lt;FORM Method=&quot;POST&quot; Action=&quot;http://www.my_server.com/cgi-bin/register.pl&quot;&gt;

&lt;P&gt;

&lt;INPUT Type=&quot;hidden&quot; Name=&quot;address&quot; Value=&quot;new_user@my_server.com&quot;&gt;

Your Name: 

&lt;INPUT Type=&quot;text&quot; Name=&quot;user-name&quot; Size=&quot;2&Oslash;&quot; Maxlength=&quot;3&Oslash;&quot;&gt;

&lt;INPUT Type=&quot;hidden&quot; Name=&quot;subject&quot; Value=&quot;new_user_info&quot;&gt;&lt;BR&gt;&lt;BR&gt;

Where did you hear about out site?:

&lt;INPUT Type=&quot;radio&quot; Name=&quot;where&quot; Value=&quot;online&quot;&gt;The Internet

&lt;INPUT Type=&quot;radio&quot; Name=&quot;where&quot; Value=&quot;television&quot;&gt;On Television

&lt;INPUT Type=&quot;radio&quot; Name=&quot;where&quot; Value=&quot;friend&quot;&gt;A Friend&lt;BR&gt;&lt;BR&gt;

Please check all the mediums you use:&lt;BR&gt;

&lt;INPUT Type=&quot;checkbox&quot; Name=&quot;television&quot;&gt;Television&lt;BR&gt;

&lt;INPUT Type=&quot;checkbox&quot; Name=&quot;internet&quot;&gt;The Internet

&lt;INPUT Type=&quot;checkbox&quot; Name=&quot;radio&quot;&gt;Radio

&lt;INPUT Type=&quot;checkbox&quot; Name=&quot;print&quot;&gt;Magazines and Newspapers

&lt;BR&gt;&lt;BR&gt;

Please choose one of our products as a gift for fillin gout this form:

&lt;SELECT Name=&quot;product_gifts&quot;&gt;

     &lt;OPTION&gt;Lead Pencil 2000

     &lt;OPTION&gt;Staple-O-Matic!

     &lt;OPTION&gt;Glue Master Sticky Tape

     &lt;OPTION&gt;Log!

&lt;/SELECT&gt;&lt;BR&gt;&lt;BR&gt;

Please choose your favourite Web browser:

&lt;SELECT Name=&quot;browsers&quot; Multiple Size=4&quot;&gt;

     &lt;OPTION Value=&quot;straight&quot;&gt;AOL

     &lt;OPTION Value=&quot;straight&quot;&gt;Explorer

     &lt;OPTION Value=&quot;hip&quot;&gt;Navigator

     &lt;OPTION Value=&quot;hip&quot;&gt;Mosaic

     &lt;OPTION Value=&quot;weird&quot;&gt;Lynx

&lt;/SELECT&gt;&lt;BR&gt;&lt;BR&gt;

&lt;H3&gt;Guess the secret word contest&lt;/H3&gt;&lt;BR&gt;

Try and guess our secret word to win a prize!&lt;INPUT Type=&quot;password&quot; Name=&quot;word_guess&quot;&gt;

&lt;BR&gt;&lt;BR&gt;

Do you like our site?:

&lt;INPUT Type=&quot;radio&quot; Name=&quot;site_feedback&quot; Value=&quot;Yes&quot; Checked&gt;Yes

&lt;INPUT Type=&quot;radio&quot; Name=&quot;site_feedback&quot; Value=&quot;No&quot;&gt;If not, then what don't you like?

&lt;BR&gt;&lt;BR&gt;

&lt;TEXTAREA Name=&quot;user_suggest&quot; Rows=&quot;4&quot; Cols=&quot;5&Oslash;&quot;&gt;

Thanks for your thoughts!

&lt;/TEXTAREA&gt;&lt;BR&gt;&lt;BR&gt;

Where are you from?&lt;INPUT Type=&quot;image&quot; Src=&quot;http://www.my_server.com/images/map.gif&quot; 

Name=&quot;user_location&quot; Align=&quot;top&quot;&gt;&lt;BR&gt;

Now that you're done, let us know by sending us your info.&lt;BR&gt;&lt;BR&gt;

&lt;INPUT Type=&quot;Submit&quot; Value=&quot;Send it in!&quot;&gt;

&lt;INPUT Type=&quot;Reset&quot; Value=&quot;Do it again!&quot;&gt;

&lt;/FORM&gt;

&lt;P&gt;

Thanks for registering!

&lt;HR&gt;

&lt;/BODY&gt;

&lt;/HTML&gt;<BR>

</PRE>

</BLOCKQUOTE>

<P>

<CENTER>

<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>

<TR VALIGN=TOP><TD WIDTH=576><B>NOTE</B></TD></TR>

<TR VALIGN=TOP><TD WIDTH=576>

<BLOCKQUOTE>

<I>To effectively use an image map like the one used in our new user form example, you must define your graphic, or image map, properly. You can do this by using a Perl script, like the one for defining image maps discussed in <A HREF="ch6.htm" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/ch6.htm" >Chapter 

6</A> Please see that script for details</I>

</BLOCKQUOTE>



</TD></TR>

</TABLE></CENTER>

<P>

<H2><A NAME="EnvironmentalVariables"><FONT SIZE=5 COLOR=#FF0000>

Environmental Variables</FONT></A></H2>

<P>

If you want to supply Web browsers with truly dynamic entities

that use the CGI to do more than just retrieve other static HTML

documents, like an educated gopher, then running executables from

the server's side of things is a must. The CGI must also be able

to take specific user's data, input for a specific task. Environmental

variables are one of the ways to do this.

<P>

To understand how environmental variables differ from regular

variables, it is important to know about scope. <I>Scope</I> refers

to the extent to which a variable is understood. Most variables

are redefined each time a program is run, most often only in a

certain block of code within that program. This is the common,

garden-variety file variable. When you get to environmental variables,

however, their value stays the same throughout each script and

application started within each CGI, or Perl window, or shell.

Their value is based upon the first document opened by the browser.

<P>

This has various implications, not the least of which is the ability

of different applications, or processes, to share the same environmental

variables across the same shell.

<P>

To illustrate environmental variables we will use some Perl scripts.

Before getting into the details of environmental variables, however,

you should know that not all environmental variables are carried

on every system. To check and see which environmental variables

are supported by your NT server, you can use System Control Panel.

The full list of System Environmental Variables is listed here,

as well as two text boxes beneath, which can be used to create

new environmental variables for your system.

<P>

You can also use this Perl script called env_var.pl to print out

the environmental variables available to your CGI scripts, as

in Figure 10.2.

<P>

<A HREF="f10-2.gif" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/f10-2.gif"><B>Figure 10.2 :</B> <I>Environmental variables available to your

server</I>.</A>

<BLOCKQUOTE>

<PRE>

#!/usr/bin/perl

     #env_var.pl

     push(@INC, &quot;/cgi-bin&quot;);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -