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:
[Solved]Post form and response on same page

 



dunsta
Novice

May 19, 2013, 12:02 AM

Post #1 of 3 (410 views)
[Solved]Post form and response on same page Can't Post

Solved, I found I needed an ';' after the html 'p' in the else section.

Hi

I would like to be able to post the response on a 'clean' page, i.e. without the initial form.

Currently my code looks like this:

Code
#!c:/wamp/bin/perl/bin/perl.exe 

use warnings;
use strict;

use CGI qw(:all);

print
header(),
start_html("Form and Response in one"),
start_form,
h1("Submitted Information"),
"Field 1", textfield('field1'),p,
"Field 2", textfield('field2'),p,
submit, reset,
hr,
end_form;


if(param()){
my @names = param();

foreach(@names){
print p(b($_), " ", param($_));
}

}

print end_html();


but if I put the @names = params() in the else, the script prints 'field1' and 'field2'.


Code
#!c:/wamp/bin/perl/bin/perl.exe 

use warnings;
use strict;

use CGI qw(:all);

unless(param()){
print
header(),
start_html("Form and Response in one"),
start_form,
h1("Submitted Information"),
"Field 1", textfield('field1'),p,
"Field 2", textfield('field2'),p,
submit, reset,
hr,
end_form;
}else{

print
header(),
start_html("head"),
h2("Info goes here"), p,
my @names = param();
foreach(@names){
print p(b($_), " ", param($_));
}

}


so I tried putting the @names declaration above the unless, but I am not sure why the script does not run this way:


Code
#!c:/wamp/bin/perl/bin/perl.exe 

use warnings;
use strict;

use CGI qw(:all);
my @names = param();

unless(@names){
print
header(),
start_html("Form and Response in one"),
start_form,
h1("Submitted Information"),
"Field 1", textfield('field1'),p,
"Field 2", textfield('field2'),p,
submit, reset,
hr,
end_form;
}else{

print
header(),
start_html("head"),
h2("Info goes here"), p,
#my @names = param();
foreach(@names){
print p(b($_), " ", names($_));
}

}

print end_html();


Thanks for any input.


(This post was edited by dunsta on May 19, 2013, 12:11 AM)


hwnd
User

May 19, 2013, 10:37 AM

Post #2 of 3 (393 views)
Re: [dunsta] [Solved]Post form and response on same page [In reply to] Can't Post

It's always good to solve your own problem, good job. Just glancing over your script, I see you're printing the header twice. Once in the unless loop and in the else loop, you only need to print that once. And for having the form and results on each a clean page, here is another idea of how you can do this.


Code
use strict; 
use warnings;
use CGI qw( :standard );

# map our input params (field1 and field2) to array @names
my @names = map { param($_) } qw(field1 field2);

# Print the header and start_html at the top only once.
print header,
start_html('Form and Response in one');

# Displaying our form called by sub 'display_form'
# and exit unless params have input

display_form() and exit unless @names;

# Looping through array @names to print results
# $_ = each field

for ( @names ) {
print b(qq(Name: )), $_, "\n";
# do something else..
}

# Subroutine to display your form.
sub display_form {
print start_form,
h1('Enter some names:'),
qq( Field 1 ), textfield('field1'), p,
qq( Field 2 ), textfield('field2'), p,
submit, reset, hr,
end_form;
}



dunsta
Novice

May 19, 2013, 5:13 PM

Post #3 of 3 (380 views)
Re: [hwnd] [Solved]Post form and response on same page [In reply to] Can't Post

Thanks for not only responding to my Question, but throwing in some tips on better coding style too.

Much Appreciated

 
 


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

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