CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate: Re: [Gayu4justin] template display: Edit Log



Zhris
Enthusiast

Mar 27, 2013, 3:38 PM


Views: 669
Re: [Gayu4justin] template display

Hey,

If I understand you correctly, you want the value of a textarea to be based on the selected option of a select menu.

Here is a very rough example, making vigorous use of the CGI module:


Code
#!/usr/bin/perl 
use strict;
use warnings FATAL => qw/all/;
use CGI;
use CGI::Carp qw/fatalsToBrowser/;

#####

my $templates = { A => 'This is template A',
B => 'This is template B',
C => 'This is template C' };

my $default_template = 'A';

my $cgi = CGI->new( );

my $template_content = $templates->{ $cgi->param('template') // $default_template } // $templates->{ $default_template };

#####

print $cgi->header( );

print $cgi->start_html( );

print $cgi->start_form( );

print $cgi->popup_menu( -name => 'template',
-values => [ sort keys %$templates ],
-default => $default_template,
-onChange => "window.location='?template='+this.value" );

print q{<noscript>} . $cgi->submit( -value => 'Update' ) . q{</noscript>};

print q{<br />} . $cgi->textarea( -name => 'template_content',
-default => $template_content,
-override => 1, # required otherwise default when script first invoked is used (sticky).
-rows => 10,
-columns => 100 );

print $cgi->end_form( );

print $cgi->end_html( );

#####

exit( );



- I have stored "templates" in a hashref which can be accessed by the value of the selected option.
- CGI's param() method can be used get the value of the selected option. I ensure if no value, i.e. first time script is invoked, or unknown value, then a default value is used.
- It would be nicer to use a template module, i.e. Template::Toolkit, rather than using CGI's methods to create HTML elements. However, my opinion is its easier for beginners to learn the CGI way first.
- I have used a javascript onChange event to reinvoke the script on option select, with the appropriate query string. If javascript is off, I have used <noscript> tags to display a submit button instead.

Apologies if my explanation goes over your head. Hope this helps,

Chris


(This post was edited by Zhris on Mar 27, 2013, 3:48 PM)


Edit Log:
Post edited by Zhris (Enthusiast) on Mar 27, 2013, 3:48 PM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives