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;

In reply to Problem With CGI::Application and FormBuilder by thind

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.