thind has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have a issue in one of the application i am developing i am using CGI::Application and CGI::FormBuilder Here in this application, in case if i dont use CGI::Application then $form->submitted check works fine. But if i use CGI::Application then $form->submitted never comes out to be true. Any suggestion what i might be doing wrong, where should i look for this. sorry for the formatting mistake that i did last time This is the Code i am trying to run
package TestProj ; use strict; use base 'CGI::Application'; use HTML::Template; use CGI::FormBuilder; use CGI::Session; sub setup { my $self = shift; $self->mode_param('rm'); $self->run_modes( 'mode1' => 'login', 'mode2' => 'firstpage', 'mode3' => 'secondpage', 'mode4' => 'logout' ); $self->start_mode('mode1'); } #Code for login page sub login { my $self = shift; my $query = $self->query(); my @fields = qw(username password rm sid); my $form = CGI::FormBuilder->new( title => 'TestProj Login Screen', header => 1, method => 'post', fields => \@fields, template => './tmpl/login.tmpl', required => 'ALL', submit => 'Login', ); $form->field(name => 'username', type => 'text'); $form->field(name => 'password', type => 'password'); $form->field(name => 'rm', type => 'hidden'); $form->field(name => 'rm', value => 'mode2'); $form->field(name => 'sid', type => 'hidden'); if($form->submitted eq 'Login') { my $username = $query->("username"); my $password = $query->("password"); #user authentication check code will go here if($username eq 'someval' && $password eq 'someval') { #create session in case user is valid my $session = new CGI::Session( undef, undef, {Directory=> +'/tmp/sess +ions'}); my $sid = $session->id(); $form->field(name => 'sid', value => $sid); $session->param('logged-in','y'); } else { #using loggedout.tmpl with message invalid user name or password my $template = HTML::Template->new(filename => './tmpl/log +gedout.tmp +l'); $template->param(reason => 'Invalid Username or Password: Login Again' + ); $template->param(link => './testproj.pl' ); print $template->output; } } else { print $form->render; } } sub firstpage { my $self = shift; my $query = $self->query; my $sid = $query->{'sid'}[0]; my $session = CGI::Session->load(undef, $sid , {Directory=>'/tmp/sessi +ons'}); print "Content-Type: text/html\n\n"; if($session->param('logged-in') eq 'y') { #Code for first page } else { #Call loggedout.tmpl with incvalid session message } } sub secondpage { my $self = shift; my $query = $self->query; my $sid = $query->("sid"); my $session = CGI::Session->load(undef, $sid , {Directory=>'/tmp/s +es +sions'}); print "Content-Type: text/html\n\n"; if($session->param('logged-in') eq 'y') { #Code for second page } else { #Call loggedout.tmpl with invalid session message } } sub logout { my $self = shift; my $query = $self->query; my $sid = $query->("sid"); my $session = CGI::Session->load(undef, $sid , {Directory=>'/tmp/s +es +sions'}); $session->flush(); $session->delete(); print "Content-Type: text/html\n\n"; #using template loggedout.tmpl with logged out message my $template = HTML::Template->new(filename => './tmpl/loggedout.t +mp +l'); #fill the loggedout.tmpl template $template->param(reason => 'Logged O +ut' ); $template->param(link => './testproj.pl' ); print $template->output; } 1;
Hi Guys, Thanks for the information, I read some more document and got to know about the problem that was causing this issue. Issue: Not associating FormBuilder object with the CGI query object ..and every time creating a new query object. Now the code looks like this (its working as it is expected) but i will still ask you a favour to please read it and let me know some suggestion that you may feel.
#!/usr/bin/perl -w package TestProj; use strict; use base 'CGI::Application'; use HTML::Template; use CGI::FormBuilder; use CGI::Session; #setting up runmodes sub setup { print STDERR "setup() : SETTING RUNMODES..."; my $self = shift; $self->run_modes( 'mode1' => 'login', 'mode2' => 'firstpage', 'mode3' => 'secondpage', 'mode4' => 'logout', ); $self->mode_param('rm'); $self->start_mode('mode1'); } #Code for login page sub login { my $self = shift; my $query = $self->query(); my @fields = qw(username password); #using form builder to display login screen my $form = CGI::FormBuilder->new( title => 'TestProj Login Screen', header => 0, method => 'post', name => 'LoginPage', fields => \@fields, submit => 'Login', params => $query, # get CGI params template => './tmpl/login.tmpl', reset => 0, ); $form->field(name => 'username', type => 'text'); $form->field(name => 'password', type => 'password'); if($form->submitted eq "Login") { print STDERR "login() : FORM SUBMITTED..."; #create session if($query->param('password') eq $query->param('username')) { print STDERR "login() : LOGIN SUCCEDED..."; #create a new session and save (logged-in => 1) in session my $session = new CGI::Session( undef, $query, {Directory= +>'/tmp/sessions'} ); my $sid = $session->id(); $session->param('logged-in', 1); #now display first page using pages.tmpl my $template = HTML::Template->new(filename => './tmpl/pag +es.tmpl', +associate => $query); $template->param(pagelink => "Second Page" ); $template->param(page => "First Page" ); $template->param(linkmode => "mode3" ); $template->param(sid => "$sid" ); return $template->output; } else { print STDERR "login() : LOGIN FAILED..."; #using loggedout.tmpl with message invalid user name or pa +ssword my $template = HTML::Template->new(filename => './tmpl/log +gedout.tmp +l', associate => $query); $template->param(reason => "Invalid Username or Password: +Login Again +" ); $template->param(link => "./testproj.pl?rm=mode4" ); return $template->output; } } else { print STDERR "Render..."; return $form->render; } } sub firstpage { my $self = shift; my $query = $self->query(); my $sid = $query->param('sid'); #load existing session using $sid don't create a new one my $session = CGI::Session->load(undef, $sid , {Directory=>'/tmp/s +es +sions'}); #check if it is a valid session if($session->param('logged-in') == 1) { #Code for first page } else { #Call loggedout.tmpl with message invalid user name or passwo +rd } } sub secondpage { my $self = shift; my $query = $self->query(); my $sid = $query->param('sid'); #load existing session using $sid don't create a new one my $session = CGI::Session->load(undef, $sid , {Directory=>'/tmp/s +es +sions'}); #check if it is a valid session if($session->param('logged-in') == 1) { #Code for second page } else { #Call loggedout.tmpl with message invalid user name or password } } sub logout { print STDERR "logout() : DELETING SESSION..."; my $self = shift; my $query = $self->query; my $sid = $query->param('sid'); my $session = CGI::Session->load(undef, $sid , {Directory=>'/tmp/ +ses +sions'}); $session->flush(); $session->delete(); #using template loggedout.tmpl with logged out message my $template = HTML::Template->new(filename => './tmpl/loggedout. +tmp +l', associate => $query); #fill the loggedout.tmpl template $template->param(reason => 'Logged O +ut' ); $template->param(link => './testproj.pl' ); return $template->output; } 1;

Replies are listed 'Best First'.
Re: Problem With CGI::Application and FormBuilder
by scorpio17 (Canon) on May 09, 2007 at 13:20 UTC
    Here's a few tips:
    Don't "use HTML::Template" - it's already included by CGI::Application.
    Don't "use CGI::Session" - go with CGI::Application::Plugin::Session instead (it's a wrapper around CGI::Session that makes using it inside CGI::Application easier)
    It looks like you're only using FormBuilder to do a login page - take a look at CGI::Application::Plugin::Authentication. It includes a nice, ready-to-use login form, which you can customize in a variety of ways, OR ignore and write your own - but all the technical details are taken care of for you.
    Take time to study the CGI.pm documentation - you have lots of raw html in your print statements - not a good idea.
    Some of these modules may have a bit of a learning curve, but they do much of the heavy lifting for you. If you ignore them, you'll be "reinventing the wheel". Good luck!
Re: Problem With CGI::Application and FormBuilder
by Limbic~Region (Chancellor) on May 09, 2007 at 12:40 UTC
    thind,
    Likely you missed my comment on this question in the CB because you were busy writing this post. It is unfortunate because, from my perspective, you have repeated the same problem here - lack of clarity.

    If you could forget for a second you know what your code looks like and what it was supposed to do and put yourself in our shoes, could you read your own question and honestly have the first clue what you were talking about?

    Why don't you start out with the basics. Explain what the project is supposed to do, provide code examples showing it working (apparently without CGI::Application) and not working (apparently with CGI::Application). We will try and help once you have provided the clarity, detail, and code required to answer your question.

    Cheers - L~R

Re: Problem With CGI::Application and FormBuilder
by derby (Abbot) on May 09, 2007 at 14:59 UTC

    In CGI::Application, you shouldn't print the html to STDOUT when in runmodes, you need to return it.

    Also, in your login runmode if the login and password are successful, you should either call the next runmode or redirect to where you want to go. Right now, you return no html so a blank page will appear.

    -derby