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:
webform: passing text field through subs

 



Iconx
Novice

Dec 9, 2014, 1:07 PM

Post #1 of 6 (3547 views)
webform: passing text field through subs Can't Post

This is a bit perplexing, I'm missing something... in summary when taking a textarea form variable it passes to the next sub but not the one after it completly, why? Example(code is simplified for briefness):


Code
sub modify{ 
print $form->font( { face => 'arial', size => 3 }, '<p align=left><b>Account Change Justification:</b></p>' );
enter_reason_for_account();

.....etc...etc...
}

sub enter_reason_for_account{
print $form->br();
print $form->p(
{ align => 'left' },
$form->textarea(
-rows => 10,
-cols => 40,
-name => 'reason_acct',
),
'<i>Enter the reason for the account</i>',
);
print $form->br();
}

Confirm {
my $reason_acct = $form->param('reason_acct');

....(do stuff here)....
print <<EOF;
Confirming your account etc...
<input type="hidden" name="reasons" value=$reason_acct>
EOF
}

Submit{
my $reason_acct = $form->param('reasons');
....(do other stuff submitting etc...)....
}


So I've included the most pertinent info in this code example I think, Modify calls a sub to get the reason and puts it in a form var called reason_acct. When I get to confirm i can still see the whole reason like "cause I need it". Even right before doing the <input type=hidden> part I can see the entire reason still.

Once it gets to submit and does the $reason_acct = $form->param('reason_acct') and I print reason acct the reason is "cause". Its chopped off the rest of the sentence "I need it". Whats going on here? what am I missing?


(This post was edited by Iconx on Dec 9, 2014, 1:21 PM)


FishMonger
Veteran / Moderator

Dec 9, 2014, 1:22 PM

Post #2 of 6 (3543 views)
Re: [Iconx] webform: passing text field through subs [In reply to] Can't Post

Broken and partial code fragments don't help and based on what you've posted there's no possible way for us to determine the problem without pure guesswork.

Please post a short but complete test script that demonstrates the problem.


(This post was edited by FishMonger on Dec 9, 2014, 1:22 PM)


Iconx
Novice

Dec 9, 2014, 2:43 PM

Post #3 of 6 (3536 views)
Re: [FishMonger] webform: passing text field through subs [In reply to] Can't Post

ok your gonna make me work for this. This code runs, but you may have to remove header and footer and anything having to do with the $LID variable, but it demonstrates the problem. If you put a multi-word entry into reason like "because I want it", by the time the script reaches the submit section the reason is only the word "because" like its being chopped somewhere. I open to other suggestions for the code as well if you want to say something.


Code
#!/usr/bin/perl -w 

use strict;
use CGI;
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use POSIX "strftime";
use Data::Dumper;
use lib "/acct/unixadm/web/lib";
use ReadFile;
use Local_Identity;

umask 002;

my @PAGES = ( 'Modify Account', 'Confirmation', 'Submit' );

# To debug, set $DEBUG = 1. For production, set $DEBUG = 0
# To disable sending email or writing to files set $DISABLE_EMAIL and/or DISABLE_WRITES to 1
my $DEBUG = 1;
my $DISABLE_EMAIL = 1;
my $DISABLE_WRITES = 1;

my $LID = Local_Identity->new();
my $THIS_SCRIPT = "reason_test.cgi";
my $BASE_HREF = $LID->get('baseurl') . 'web/' . $LID->get('login') . '/cgi-bin/';
my $THIS_URL = $BASE_HREF . $THIS_SCRIPT;
my $HORZ_LINE = '_' x 77 . "\n\n";
my $FOOTER_HTML = $LID->get('footer');
my $HEADER_HTML = $LID->get('header');
my $IDNUM = "12345";

######################################################################
my $form = new CGI;
my $page_param = $form->param('page') || undef;
my $go_param = $form->param('go');
my $app_value = $form->param('ApplicationParam') || "SomeApplicationValue";
my $current_page = &calculate_page( $page_param, $go_param );
my $page_name = $PAGES[$current_page];

&print_header();
if ( !defined $page_param ) {
print "page: undef, go: undef \n" if ($DEBUG);
print "current_page: $current_page, page_name: $page_name \n" if ($DEBUG);
}
else {
print "page_param: $page_param, go_param: $go_param, \n" if ($DEBUG);
print "current_page: $current_page, page_name: $page_name \n" if ($DEBUG);
}

&dump_account_params() if ($DEBUG);
&print_form( $current_page);
&print_footer();

exit 0;

######################################################################
#
# calculate_page subroutine: calculate which page to generate
#
######################################################################

sub calculate_page {
my ( $prev, $direct ) = @_;

open( TAB, ">>/tmp/errors" );
if ( defined $direct ) {
print TAB "direct: $direct page: $prev\n" unless ($DISABLE_WRITES);
}
else {
print TAB "direct: undefined page: undefined\n" unless ($DISABLE_WRITES);
}
close TAB;

return (1) if ( !defined $prev );
return (1) if $direct eq 'MODIFY selected account';
return (2) if $direct eq 'Submit Request';
return (3) if $direct eq 'Submit';
}

######################################################################
#
# print_form subroutine
#
######################################################################

sub print_form {
my ( $current_page, $DRIVER_BEMSID ) = @_;

print $form->start_form( -METHOD => "POST" );

&Modify_account($IDNUM) if ( $current_page == 1 );
&Confirm($IDNUM) if ( $current_page == 2 );
&Submit($IDNUM) if ( $current_page == 3 );
}

######################################################################
#
# print_header subroutine
#
######################################################################

sub print_header {
print $form->header();

&Header;

print $form->start_form(
-METHOD => "POST",
-action => "$THIS_URL"
);

print <<EOF ;
<SCRIPT LANGUAGE="JavaScript">
<!-- hide this script tag's contents from old browsers
function goHist(a)
{
history.go(a); // Go back one.
}
//<!-- done hiding from old browsers -->
</script>
<SCRIPT language="JavaScript" type="text/javascript">
function closeWindow() {
var thisWindow = window.top;
thisWindow.opener = window.top;
thisWindow.close();
}
var win=null;
</SCRIPT>
<script language="javascript" type="text/javascript">
<!--
/****************************************************
Popup window generator provided for free at Hypergurl
Url: http://www.hypergurl.com
Author: Eric King
Url: http://eak.digitalrice.com
This script is free to use as long
as this info is left in
****************************************************/
var win=null;
function NewWindow(mypage,myname,w,h,scroll,pos){
if(pos=="random"){LeftPosition=(screen.availWidth)?Math.floor(Math.random()*(screen.availWidth-w)):50;TopPosition=(screen.availHeight)?Math.floor(Math.random()*((screen.availHeight-h)-75)):50;}
if(pos=="center"){LeftPosition=(screen.availWidth)?(screen.availWidth-w)/2:50;TopPosition=(screen.availHeight)?(screen.availHeight-h)/2:50;}
if(pos=="default"){LeftPosition=50;TopPosition=50}
else if((pos!="center" && pos!="random" && pos!="default") || pos==null){LeftPosition=0;TopPosition=20}
settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',location=yes,directories=no,status=no,menubar=no,toolbar=no,resizable=yes';
win=window.open(mypage,myname,settings);
if(win.focus){win.focus();}}
// -->
</script>
EOF
print <<EOF;
<SCRIPT language="JavaScript" type="text/javascript">
function closeWindow() {
var thisWindow = window.top;
thisWindow.opener = window.top;
thisWindow.close();
}
</SCRIPT>
EOF

}

######################################################################
#
# print_footer subroutine
#
######################################################################

sub print_footer {
print "<p>{print_footer} Sub</p>\n" if ($DEBUG);

print "<br>\n</div>";
open( FOOTER_HTML, "<$FOOTER_HTML" );
while (<FOOTER_HTML>) {
print "$_";
}
close FOOTER_HTML;
print "<br>\n</div>";
print $form->end_html();
}

######################################################################
#
# dump_account_params subroutine: Print out account parameters
#
######################################################################

sub dump_account_params {
print "<p>{dump_account_params} Sub</p>\n" if ($DEBUG);

my $dump = $form->Dump();
print "<p align=left>$dump</p>\n";
print "This URL = $THIS_URL<br><br>\n";
}



sub Modify_account {
print "<p>{Modify_account} Sub</p>\n" if ($DEBUG);


print $form->font( { face => 'arial', size => 3 }, '<p align=left><b>Account Change Justification:</b></p>' );
enter_reason_for_account();
print "<p>Modify_account</p>\n" if ($DEBUG);

print $form->hidden(
-name => 'page',
-value => $current_page,
-override => 1
);

print $form->center(
$form->table(
{ -border => 0, cellpadding => 5 },
$form->Tr(
$form->td(
$form->submit(
-NAME => 'go',
-VALUE => 'Submit Request',
)
),
$form->td( $form->reset( -NAME => 'Reset' ) ),
$form->td(
$form->button(
-NAME => 'go',
-VALUE => 'Back',
-ONCLICK => "javascript:history.back( );"
)
),
$form->td( $form->submit( -NAME => 'go', -VALUE => 'Cancel' ) ),
$form->td( $form->submit( -NAME => 'go', -VALUE => 'Delete' ) ),
)
)
);
}


#####################################################################
#
# enter_reason_for_account subroutine: Identify who will be the approving manager
#
######################################################################

sub enter_reason_for_account {
print "<p>{enter_reason_for_account} Sub</p>\n" if ($DEBUG);

print $form->br();
print $form->p(
{ align => 'left' },
$form->textarea(
-rows => 10,
-cols => 40,
-name => 'reason_acct',
),
'<i>Enter the reason for the account</i>',
);
print $form->br();
}

######################################################################
#
# Confirm subroutine: Confirm request information
#
######################################################################

sub Confirm {
print "<p>{Confirm} Sub</p>\n" if ($DEBUG);


my $reason_acct = $form->param('reason_acct');
my $source_page = $form->param('page_src');

print "CONFIRM REASON: $reason_acct <br>";

if ( $reason_acct eq "" ){
print "<p><font color='red' face=arial size=4><b><i>";
print "The reason for the account was not entered. ";
print "Please type your justification and resubmit";
print "</i></b></font></p>\n";
&Edit;
return;
}

print <<EOF;
<tr><td colspan=2>&nbsp;<br></td></tr>
<tr><td colspan=2>&nbsp;<br></td></tr>
<tr>
<td colspan=2 align=center>
<input type="hidden" name="reasons" value=$reason_acct>
<input type="hidden" name="page" value=$current_page override=1>
<p><font color='red' face=arial size=4><b><i>
If you do not use your account for 90 Days account will be disabled and at 180 Days deleted without notification.
</i></b></font></p>
<input type="submit" name="go" value="Submit">&nbsp; &nbsp;
<input type='button' name='go' value='Modify' onclick='history.go(-2)'> &nbsp; &nbsp;
<input type="submit" name="go" value="Quit">&nbsp;
</td>
</tr>
</table>
EOF
}

######################################################################
#
# Submit subroutine: Save request data and send email notices
#
######################################################################

sub Submit {
print "<p>{Submit} Sub</p>\n" if ($DEBUG);

my $reason_acct = $form->param('reasons');

print "REASON in SUBMIT: $reason_acct<br>";
print "Account modification has been submitted. Other stuff will go here <br>";

&save_reason_acct( $reason_acct );
}

######################################################################
#
# Edit subroutine
#
######################################################################

sub Edit {
print "<p>{Edit} Sub</p>\n" if ($DEBUG);

print "<p align=center><font face=arial size=3>";
print "Click the 'Edit' button to return to your request to edit it.</p>\n";
print "<p align=center><input type='button' value=' Edit ' onclick='history.go(-1)'</p>\n";
&print_footer();
}

######################################################################
#
# save_reason_acct subroutine: Save an entry to an account record
#
######################################################################

sub save_reason_acct {
my ( $reason_acct, ) = @_;
print "<p>{save_reason_acct} Sub</p>\n" if ($DEBUG);
print "reason is $reason_acct<br>";
print "<br>Writting of reason to file would go here <br>";
}

######################################################################
#
# Header subroutine
#
######################################################################

sub Header {

open( HEADER, "<$HEADER_HTML" ) or do { print("Could not open $HEADER_HTML: $!"); exit(-1); };
while (<HEADER>) {
print("$_");
}
close(HEADER);
print <<EOF;
<div id="content">
<div id="breadCrumb">
Account Modification Form
<!-- ### END BREADCRUMBS ### -->
</div>
<!-- ### BEGIN MAIN TITLE ### -->
<div class="content_intro">
<h2> Account Modification Form</h2>
</div>
<!-- ### END MAIN TITLE ### -->

<div class="content_feat">
<hr width=600 noshade color=#004594>
EOF
}

__END__



FishMonger
Veteran / Moderator

Dec 10, 2014, 7:44 AM

Post #4 of 6 (3506 views)
Re: [Iconx] webform: passing text field through subs [In reply to] Can't Post

Sorry for the delay in responding.

I looked over your code a number of times and I have to be honest. It is extremely convoluted and uses a lot of poor coding practices. Those 2 facts makes you code difficult to read, follow its logic and to troubleshoot. I almost decided not to bother troubleshooting, but did it anyway.

After a few tests I discovered that the problem you're having is due to your lack of proper quoting of the html attributes. Specifically the value=somevalue sections.

Change this section of the Confirm sub:

Code
<input type="hidden" name="reasons"      value=$reason_acct> 
<input type="hidden" name="page" value=$current_page override=1>


To this:

Code
<input type="hidden" name="reasons" value="$reason_acct"> 
<input type="hidden" name="page" value="$current_page" override=1>


Personally, I'd throw that whole thing away and start over from scratch.


Iconx
Novice

Dec 10, 2014, 9:41 AM

Post #5 of 6 (3501 views)
Re: [FishMonger] webform: passing text field through subs [In reply to] Can't Post

Yea I've been cleaning up this code that somebody else wrote but I dont know of another better way to do multi-pages. Thanks for the help, that worked.


(This post was edited by Iconx on Dec 10, 2014, 9:44 AM)


FishMonger
Veteran / Moderator

Dec 10, 2014, 10:01 AM

Post #6 of 6 (3493 views)
Re: [Iconx] webform: passing text field through subs [In reply to] Can't Post

If I was the one working on this script (and any related ones), I'd rewrite it using one of Perl's web frameworks such as Dancer or Mojolicious which runs under PSGI instead of CGI. That will allow you to do a dramatic cleanup and very easily handle the multiple pages. It would also be a much more responsive (faster) site.
http://search.cpan.org/~xsawyerx/Dancer2-0.156001/lib/Dancer2.pm
http://search.cpan.org/~sri/Mojolicious-5.68/lib/Mojolicious.pm

Staying with CGI, then at the very least you should use one of the template modules. HTML::Template would probably be the easiest to start with.
http://search.cpan.org/~wonko/HTML-Template-2.95/lib/HTML/Template.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