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: Beginner:
return to the same page after submit?

 



rekcut01
Novice

May 23, 2014, 8:41 AM

Post #1 of 8 (7840 views)
return to the same page after submit? Can't Post

Friends,

I know I ask a lot of questions but I do try several methods before I post as to try and not pester people. My latest challenge is to return to the same page with the text fields cleared out after submit. I have used the following lines:

print '<script language="JavaScript">';
print '<javascript:window.history.back(1);>';
print '</script>';

and this works... sort of, but it doesn't reset the fields to the original state, how is this done? All the examples I have tried seem not to work for me.

Thanks for the help

Rick


Zhris
Enthusiast

May 24, 2014, 11:53 AM

Post #2 of 8 (7299 views)
Re: [rekcut01] return to the same page after submit? [In reply to] Can't Post

Hi,

How about a redirect header (don't print any other header beforehand):


Code
use CGI; 
my $q = CGI->new();
print $q->redirect('http://www.example.com/theform.html');
exit();


Idealistically, you would call the function that processes the form data, then the function that outputs the form, removing the need for a redirect altogether.

Chris


(This post was edited by Zhris on May 24, 2014, 12:03 PM)


rekcut01
Novice

Jun 4, 2014, 8:09 AM

Post #3 of 8 (5981 views)
Re: [rekcut01] return to the same page after submit? [In reply to] Can't Post

Thanks Chris,

Sorry it has been a while responding and thank you for your help... When trying this method I get the following error:

HTTP/1.1 302 Server: Apache/2.0.65 (Win32) Status: 302 Date: Wed, 04 Jun 2014 17:02:20 GMT Location: http://localhost/cgi_bin/login_rev3.html

Can you suggest something?

I understand what you are saying about calling a sub, process the data and call the sub again, I'm going to try to do that.



Thanks

Rick


FishMonger
Veteran / Moderator

Jun 4, 2014, 8:20 AM

Post #4 of 8 (5974 views)
Re: [rekcut01] return to the same page after submit? [In reply to] Can't Post

You should provide more details on what you're wanting to accomplish and post a short but complete script that demonstrates the problem.

Based on the minimum info you've given, I'd say you're probably wanting to use the -nosticky pragma.


Quote
-nosticky

By default the CGI module implements a state-preserving behavior called "sticky" fields. The way this works is that if you are regenerating a form, the methods that generate the form field values will interrogate param() to see if similarly-named parameters are present in the query string. If they find a like-named parameter, they will use it to set their default values.

Sometimes this isn't what you want. The -nosticky pragma prevents this behavior. You can also selectively change the sticky behavior in each element that you generate.


If that's not what you want, then maybe you need to add a button that executes a javascript function that clears the desired field values.


(This post was edited by FishMonger on Jun 4, 2014, 8:22 AM)


rekcut01
Novice

Jun 4, 2014, 11:03 AM

Post #5 of 8 (5890 views)
Re: [FishMonger] return to the same page after submit? [In reply to] Can't Post

I appreciate your input FishMonger, sorry I was vague. Here is my code, what I have done (and it works) is as follows:

get name

division or vendor

citizenship

sponsor or guide

put it in a spread sheet with a time stamp

create and print a sticky badge with a barcode

place all in a excel sheet.

this is all done after submit

but I want it to return back to original page when for the next visitor after the previous visitor hits submit.

when the visitor scans out, log out time in an excel sheet.

Here is my code:

#!c:/strawberry/perl/bin/perl.exe
# PERL MODULES WE WILL BE USING
use strict;
use warnings;
use integer;
use CGI;
use Win32::OLE;
use Spreadsheet::ParseExcel;
use Spreadsheet::ParseExcel::SaveParser;
use Spreadsheet::WriteExcel;
use Time::localtime;
use Time::Piece;
use PostScript::MailLabels 2.0;
use Spreadsheet::WriteExcel;
use Term::ReadKey;
use LWP 5.64;
use File::Copy;



print "Content-type: text/html\n\n";
# Config DB variables
our $q = new CGI;
my $TxtOrg;
my $TxtVisitReason;
my $TxtEscort;
my $TxtCit;

#Get the parameter from your html form.
my $TxtName=$q->param('TxtName');
my $TxtOrg1=$q->param('Select Division');
if ($TxtOrg1 eq "0")
{
$TxtOrg = "Other please specify";
#open a pop up window and allow visitor to input
}
elsif ($TxtOrg1 eq "1")
{
$TxtOrg = "GSD AMS";
}
elsif ($TxtOrg1 eq "2")
{
$TxtOrg = "GSD APJ";
}
elsif ($TxtOrg1 eq "3")
{
$TxtOrg = "GSD EMEA";
}
elsif ($TxtOrg1 eq "4")
{
$TxtOrg = "GSD Customer Solution Center";
}
elsif ($TxtOrg1 eq "5")
{
$TxtOrg = "GSD Supply Chain";
}
elsif ($TxtOrg1 eq "6")
{
$TxtOrg = "Delivery Readiness";
}
elsif ($TxtOrg1 eq "7")
{
$TxtOrg = "Business Process, Customer Experience and Quality";
}
elsif ($TxtOrg1 eq "8")
{
$TxtOrg = "Business Operations";
}
elsif ($TxtOrg1 eq "9")
{
$TxtOrg = "GSD Engineering Resolution";
}
elsif ($TxtOrg1 eq "10")
{
$TxtOrg = " GSD Escalation Management";
}
elsif ($TxtOrg1 eq "11")
{
$TxtOrg = "Finance, Human Resources, L and D, Others";
}


my $TxtVisitReason1=$q->param('visit reason');
if ($TxtVisitReason1 eq "0")
{
$TxtVisitReason = "Other please specify";
}
elsif ($TxtVisitReason1 eq "1")
{
$TxtVisitReason = "IDSS Sales Training";
}
elsif ($TxtVisitReason1 eq "2")
{
$TxtVisitReason = "HP Internal Meeting";
}
elsif ($TxtVisitReason1 eq "3")
{
$TxtVisitReason = "Customer Meeting";
}
elsif ($TxtVisitReason1 eq "4")
{
$TxtVisitReason = "Interview";
}
elsif ($TxtVisitReason1 eq "5")
{
$TxtVisitReason = "***************************************************************";
}

my $TxtEscort1=$q->param('Sponsor Name');
if ($TxtEscort1 eq "0")
{
$TxtEscort = "Other";
}
if ($TxtEscort1 eq "1")
{
$TxtEscort = "Mike Eberhardt";
}
if ($TxtEscort1 eq "2")
{
$TxtEscort = "Paul Ward";
}
if ($TxtEscort1 eq "3")
{
$TxtEscort = "Stephen Marvin";
}
if ($TxtEscort1 eq "4")
{
$TxtEscort = "Tom Heald";
}
if ($TxtEscort1 eq "5")
{
$TxtEscort = "Patty Wright";
}
if ($TxtEscort1 eq "6")
{
$TxtEscort = "Phil Parker";
}
if ($TxtEscort1 eq "7")
{
$TxtEscort = "Chris Southworth";
}
if ($TxtEscort1 eq "8")
{
$TxtEscort = "Brandon Cook";
}
if ($TxtEscort1 eq "9")
{
$TxtEscort = "***************************************************************";
}

my $TxtCit1=$q->param('Country code');
if ($TxtCit1 eq "840")
{
$TxtCit = "United States";
}
else
{
$TxtCit = $TxtCit1;
}

my $now_string = localtime;
my $date = localtime->strftime('%m/%d/%y');
my $bc_number = int rand 1000000000;
my $pg_path = 'C:/zint/zint.exe --height=20 -o C:/users/rtucker/desktop/vbadgeBC.png -d ';
my $sys_Command = join '', $pg_path, $bc_number;


if ($TxtName eq "")
{
print "Error: Must enter Name\n";
print "<input type='button' value='Back' onclick='javascript:history.back(1);' />";
}
elsif ($TxtOrg eq "")
{
print "Error: Must enter Organization\n";
print "<input type='button' value='Back' onclick='javascript:history.back(1);' />";
}
elsif ($TxtCit eq "")
{
print "Error: Must enter Citizenship\n";
print "<input type='button' value='Back' onclick='javascript:history.back(1);' />";
}
#elsif ($TxtHPEmp eq "")
#{
# print "Error: Must Confirm HP Employment\n";
# print "<input type='button' value='Back' onclick='javascript:history.back(1);' />";
#}
elsif ($TxtEscort eq "")
{
print "Error: Must enter Escort Name\n";
print "<input type='button' value='Back' onclick='javascript:history.back(1);' />";
}
else
{
my ($firstname, $lastname) = split " ", $TxtName;
my $rep = 'Representing: ';
my $rep_string = join '', $rep, $TxtOrg;

my $status = system($sys_Command);

# -------> CREATE BADGE HERE <-------

my $image_file_full = 'C:/Users/Rtucker/Desktop/vbadgeBC.png';
my $logo_file = 'C:/Users/Rtucker/Desktop/hplogo.png';
my $badge_pdf = 'C:/Users/Rtucker/Desktop/vBadge.pdf';
my $workbook3 = Spreadsheet::WriteExcel->new('/Users/Rtucker/Desktop/vBadge.xls');
die "Problems creating new Excel file: $!" unless defined $workbook3;

## Get the first worksheet.
my $sponsor = 'Sponsor: ';
my $sponsor_name = join '', $sponsor, $TxtEscort;


# Write over the existing file or write a new file.

## Get the first worksheet.
my $height='2.13"';
my $width='3.97"';
my $worksheet3 = $workbook3->add_worksheet(0);
my $format1 = $workbook3->add_format();
$format1->set_size(38);
$format1->set_bold();
my $format2 = $workbook3->add_format();
$format2->set_size(18);
$format2->set_bold();
my $format3 = $workbook3->add_format();
$format3->set_size(12);
my $format4 = $workbook3->add_format();
$format4->set_size(12);
my $format5 = $workbook3->add_format();
$format5->set_size(12);
$worksheet3->print_area('A1:E7');
$worksheet3->set_margins(.0);
$worksheet3->set_landscape();
$worksheet3->set_paper(1);
$worksheet3->hide_gridlines(2);
$worksheet3->write(0, 0, $firstname, $format1);
$worksheet3->write(1, 0, $lastname, $format2);
$worksheet3->write(2, 0, $rep_string, $format3);
$worksheet3->write(2, 3, $date, $format4);
$worksheet3->write(3, 0, $sponsor_name, $format5);
$worksheet3->insert_image(5, 0, $image_file_full, 0, 0, .75, 0.55);
$workbook3->close();

# -------> END CREATE BADGE HERE <-------




# -------> CREATE REPORT HERE <-------
# Open an existing file with SaveParser
my $parser = Spreadsheet::ParseExcel::SaveParser->new();
my $workbook = $parser->Parse('/Users/Rtucker/Desktop/Report.xls');

if ( !defined $workbook )
{
die $parser->error(), ".\n";
}

## Get the first worksheet.
my $worksheet = $workbook->worksheet(0);
my ( $row_min, $row_max ) = $worksheet->row_range();
my $row = $row_max + 1;
my $col = 0;
my $cell = $worksheet->get_cell( $row, $col );

$worksheet->AddCell( $row, $col, $TxtName );
$worksheet->AddCell( $row, $col + 1, $TxtOrg );
$worksheet->AddCell( $row, $col + 2, $TxtCit );
$worksheet->AddCell( $row, $col + 3, $TxtVisitReason );
$worksheet->AddCell( $row, $col + 4, $sponsor_name );
$worksheet->AddCell( $row, $col + 5, $now_string );
$worksheet->AddCell( $row, $col + 6, $bc_number );

# Write over the existing file or write a new file.
$workbook->SaveAs('/Users/Rtucker/Desktop/Report.xls');

# -------> END CREATE REPORT HERE <-------



use CGI qw(redirect referer); <------------------------ here is where I want it to return to the previous page
print redirect(referer()); <----------------------



I can't seem to get this to work no matter what I do.

As always thanks in advance

Rick

}


FishMonger
Veteran / Moderator

Jun 4, 2014, 11:54 AM

Post #6 of 8 (5878 views)
Re: [rekcut01] return to the same page after submit? [In reply to] Can't Post

Your script maybe functional, but it's also very poorly written and has too many issues for me to cover in detail.

Since the script already generated html output, you can't do a redirect.

You can have a "Go Back" link which is simply a normal link to the form page (without using javascript to use the history to move back). If you do this then you'll want to use the -nosticky option I previously mentioned.


rekcut01
Novice

Jun 4, 2014, 12:34 PM

Post #7 of 8 (5867 views)
Re: [FishMonger] return to the same page after submit? [In reply to] Can't Post

Thank you for your input.



Rick


Laurent_R
Veteran / Moderator

Jun 4, 2014, 2:48 PM

Post #8 of 8 (5823 views)
Re: [rekcut01] return to the same page after submit? [In reply to] Can't Post

I can't go through your whole program, but let me just give you at least one clue on how to write more efficient code.

This piece of code:

Code
my $TxtVisitReason1=$q->param('visit reason'); 
if ($TxtVisitReason1 eq "0")
{
$TxtVisitReason = "Other please specify";
}
elsif ($TxtVisitReason1 eq "1")
{
$TxtVisitReason = "IDSS Sales Training";
}
elsif ($TxtVisitReason1 eq "2")
{
$TxtVisitReason = "HP Internal Meeting";
}
elsif ($TxtVisitReason1 eq "3")
{
$TxtVisitReason = "Customer Meeting";
}
elsif ($TxtVisitReason1 eq "4")
{
$TxtVisitReason = "Interview";
}
elsif ($TxtVisitReason1 eq "5")
{
$TxtVisitReason = "***************************************************************";
}


could be replaced with this much shorter, faster and clearer code using an array:

Code
my @tranco_table = ("Other please specify",  "IDSS Sales Training",  "HP Internal Meeting"; 
"Customer Meeting", "Interview",
"***************************************************************");
my $TxtVisitReason1=$q->param('visit reason');
$TxtVisitReason = $transco_table[$TxtVisitReason1];


Or you could use a hash (a bit longer syntax and very slightly slower, but maybe clearer to the maintainer):

Code
my %tranco_hash = (0 => "Other please specify", 
1 => "IDSS Sales Training",
2 => "HP Internal Meeting";
3 => "Customer Meeting",
4 => "Interview",
5 => "***************************************************************");
my $TxtVisitReason1=$q->param('visit reason');
$TxtVisitReason = $transco_hash{$TxtVisitReason1};

If you do that on all your long if..elsif alternatives, you'll reduce your code by at least half, which is likely to reduce the potential for bugs by the same factor and make your code much easier to read for others.

Even when it comes to running code depending on the numerous if..elsif alternatives, it would be much better to store your code in an array or a hash, to build what is called a dispatch table. This might be a slightly more advanced topic, but it is really very easy, there is nothing complicated in it. We can show you if you are interested. But first show us your code modified for the simpler cases as per the example above, I will not take the time to show you that if you don't do the homework first
;-)

 
 


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

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