A Perl CGI (CGI.pm) textfield form example

Here's the source code for a Perl CGI textfield example (a Perl form example). The Perl code below shows how you can display an HTML form with a textfield using the Perl CGI.pm module.

The first time this script is called it displays a textfield in a form. After you submit the form, this script displays the text that you entered in the textfield.

Here's the source code for this CGI.pm "Perl form" textfield example script:

#!/usr/bin/perl -Tw
#
#  textfield.cgi - demonstrate a simple textfield form using Perl CGI.pm
#
#  Created by alvin alexander, devdaily.com.
#

#-----------------------------------#
#  1. Create a new Perl CGI object  #
#-----------------------------------#

use CGI;
$query = new CGI;

#----------------------------------#
#  2. Print the doctype statement  #
#----------------------------------#

print $query->header;

#----------------------------------------------------#
#  3. Start the HTML doc, and give the page a title  #
#----------------------------------------------------#

print $query->start_html('My textfield.cgi program');

#--------------------------------------------------------------#
#  4a.  If the program is called without any params, print     #
#  the textfield form.  Start the form, create the textfield,  #
#  create the submit button, and end the form.                 #
#--------------------------------------------------------------#

if (!$query->param) {

	print $query->startform;
	print $query->textfield(-name=>'TEXT_FIELD',
		-default=>'[Enter your text here]',
		-size=>35,
		-maxlength=>50);
	print $query->br;
	print $query->submit(-value=>'Submit your text');
	print $query->endform;

} else {

	#----------------------------------------------------------#
	#  4b.  If the program is called with parameters, retrieve #
	#  the 'TEXT_AREA' data into a variable named $yourText,   #
	#  then print that information after an <H3> tag.          #
	#----------------------------------------------------------#

	print $query->h3('Here\'s what I think you entered:');
	$yourText = $query->param('TEXT_FIELD');
	print "<BLOCKQUOTE>\n";
	print $yourText;
	print "</BLOCKQUOTE>\n";

}

#-------------------------#
#  5. End the HTML page.  #
#-------------------------#

print $query->end_html;

See this Perl CGI (CGI.pm) textfield form example in action

You can see this Perl CGI.pm textfield example (Perl form example) in action at this url.