<FORM> Submit question with perl cgi scripts

D

dogdog

Another question, guess I could start a daily question
roster since I've been posting so much lately.

Here goes.

I have my HTML document setup to submit to a cgi script.
The script passes information to my server and responds
back as it should. However, I would like to be able to
do the following:

1. Have the response post back to my original webpage and
not create a seperate page with the information.
2. If 1 is possible, I would like to have the items returned
to the original webpage placed into the textboxes that I have
setup. These boxes are used when I input information to the
server I have and hit submit.

Any way to do this ?? Would appreciate suggestions, examples
anything in relation to this.

Thanks
dogdog
 
G

Gunnar Hjalmarsson

I have my HTML document setup to submit to a cgi script. The script
passes information to my server and responds back as it should.
However, I would like to be able to do the following:

1. Have the response post back to my original webpage and not create
a seperate page with the information. 2. If 1 is possible, I would
like to have the items returned to the original webpage placed into
the textboxes that I have setup. These boxes are used when I input
information to the server I have and hit submit.

Have the script generate the empty HTML form when invoked without
arguments, and have it include the items when invoked via the submit
button.
 
D

dogdog

CGI.pm is well suited for that. By default, its methods for producing
form elements will read the values it assigns to those elements from the
incoming request.


All is revealed in the docs for CGI.pm:

perldoc CGI

or

<http://stein.cshl.org/WWW/software/CGI/>

sherm--
Sherm,
I was just reading about cgi.pm in the mouse or rat book
by o'reily (whatever its called). It seems this will work
but I'm still not 100 percent clear. From what I'm reading it
seems I will
still be generating a seperate html page and moving away from
my original index page? Is that true?

What about using embperl. Do I get any benefit by including
my scripts in the html? I guess I would still need cgi.pm to
generate the output html so it looks like the original page?

Ahh the questions, it would be so much easier if I was able to
see this in action on the web.

thanks and let me know if you have answers to even more of my
questions.
tia
dogdog
 
D

dogdog

Have the script generate the empty HTML form when invoked without
arguments, and have it include the items when invoked via the submit
button.
Gunnar,
Sorry to sound dense on this but I'm not very up to speed as
it is. I dont understand what your saying by empty HTML. Could
you elaborate.

tks
dogdog
 
G

Gunnar Hjalmarsson

Sorry to sound dense on this but I'm not very up to speed as it is. I
dont understand what your saying by empty HTML. Could you elaborate.

Okay, here is a quick-and-dirty script to illustrate the idea:

#!/usr/bin/perl -T
use strict;
use warnings;
use CGI 'escapeHTML';
use File::Basename;

my $script = basename $0;
my $q = new CGI;
my %in = $q->Vars;
for ('name', 'age') {
$in{$_} ||= '';
$in{$_} = escapeHTML $in{$_};
}

print $q->header;
print <<HTML;
<html>
<body>
<form action="$script" method="post">
Name: <input type="text" name="name" value="$in{name}"><br>
Age: <input type="text" name="age" value="$in{age}"><br>
<input type="submit">
</body>
</html>
HTML

__END__
 
A

A. Sinan Unur

(e-mail address removed) wrote in
Ahh the questions, it would be so much easier if I was able to
see this in action on the web.

There is no point in trying to do CGI development without a proper test
bed. Install Apache or another comparable web server on your development
system along with Perl and try out the examples.

And please try to keep your posts on topic. Have you read the posting
guidelines for comp.lang.perl.misc?
 
S

Sherm Pendley

I was just reading about cgi.pm in the mouse or rat book
by o'reily (whatever its called).

They call it a mouse. I like your rat idea better. :)
still be generating a seperate html page and moving away from
my original index page? Is that true?

Yeah - there's really no way around that with CGI. You can't modify the
page in-place, all you can do is send a new page.

What you *can* do though, is send a page that's virtually identical to
the one with the form.
Ahh the questions, it would be so much easier if I was able to
see this in action on the web.

Here's a simple example:

#!/usr/bin/perl -T
use strict;
use warnings;
use CGI;

my $q = new CGI;

print $q->header;

my $formtag = $q->start_form;
my $nametag = $q->textfield('name');

print <<HTML;
<html>
<body>
$formtag
What's your name? $nametag<br>
<input type="submit">
</body>
</html>
HTML

When you use the textfield() method above, it looks for an input named
'name' in the query. If it finds one, it uses it to generate a value=""
attribute for the <input ...> element.

Similarly, the start_form() method produces a <form ...> element with an
action="" method that refers back to this script.

That's the basic idea - the CGI.pm methods that produce form elements
take the values assigned to those elements from the form input. This
makes it easy to build self-referential forms like you're describing.

sherm--
 
D

dogdog

They call it a mouse. I like your rat idea better. :)


Yeah - there's really no way around that with CGI. You can't modify the
page in-place, all you can do is send a new page.

What you *can* do though, is send a page that's virtually identical to
the one with the form.


Here's a simple example:

#!/usr/bin/perl -T
use strict;
use warnings;
use CGI;

my $q = new CGI;

print $q->header;

my $formtag = $q->start_form;
my $nametag = $q->textfield('name');

print <<HTML;
<html>
<body>
$formtag
What's your name? $nametag<br>
<input type="submit">
</body>
</html>
HTML

When you use the textfield() method above, it looks for an input named
'name' in the query. If it finds one, it uses it to generate a value=""
attribute for the <input ...> element.

Similarly, the start_form() method produces a <form ...> element with an
action="" method that refers back to this script.

That's the basic idea - the CGI.pm methods that produce form elements
take the values assigned to those elements from the form input. This
makes it easy to build self-referential forms like you're describing.

sherm--

I thought that sending a somewhat identical page was what you were
eluding to. I'll read over more in the book and use your
example. Thanks alot for that its a great help.

dogdog
 
D

dogdog

You don't seem to have understood Gunnar's and Sherm's answers, so
there seems to be a conceptual disconnect. I will try to fill in some
of the blanks, although I will simplify somewhat.

You cannot in general add information to a web page that is already
displayed in your browser. (The exceptions are if you have a Java
applet, Javascript, or other browser-based method dynamically changing
the appearance of the web page.) It sounds like you have a static home
page (index.html) that contains a form, which you would like to submit
to a server. It is not clear if this server is the web server or
another, secondary server. You then want to get a response from this
server that the browser uses to modify the form elements or other
elements on the page.

What you can do is have your cgi program return a page that looks
exactly like your static home page with the form response information
included therein. Then it will appear as if your page has been updated
using your server response.

If that is what you want, you can take it one step further and arrange
to have your original home page generated by the same cgi program. This
is what Gunnar means by an empty HTML. The first time the page is
fetched and displayed, the forms elements are empty. If you get fancy,
you can fill them with default values or the values from a previous
fetch.

Please understand that this problem has little to do with Perl, and is
the same regardless of the language you use to implement the cgi
program. There are HTML groups to discuss this sort of thing. However,
Perl's CGI module is ideal for this sort of thing, hence Sherm's
recommendation to use it.

HTH.
Jim,
Thanks for your clarification on this. I went to the perl group
first because I was hoping that there was something in perl
that could be done outside of the html range of things
(i.e. fancy scripting tricks that are similiar to javascript
type page in place actions).
Thanks again for your clarification. Gunnar has also
submitted some samples in reference to what he was speaking of
earlier. Which I'm sure will be quite beneficial.
dogdog
 
D

dogdog

Okay, here is a quick-and-dirty script to illustrate the idea:

#!/usr/bin/perl -T
use strict;
use warnings;
use CGI 'escapeHTML';
use File::Basename;

my $script = basename $0;
my $q = new CGI;
my %in = $q->Vars;
for ('name', 'age') {
$in{$_} ||= '';
$in{$_} = escapeHTML $in{$_};
}

print $q->header;
print <<HTML;
<html>
<body>
<form action="$script" method="post">
Name: <input type="text" name="name" value="$in{name}"><br>
Age: <input type="text" name="age" value="$in{age}"><br>
<input type="submit">
</body>
</html>
HTML

__END__
Gunnar,

Thanks alot for the example. Its great when a person can get
an answer like yours and Sherms with some helpful examples.
I can then use them along with what is in text and on the
web to assist in clarifying things and gain a better understanding.
thanks alot
dogdog
 
T

Tad McClellan

[ snip TOFU foolishness, learn how to properly quote followups. ]

Thanks alot for the example. Its great when a person can get
an answer like yours and Sherms with some helpful examples.


STOP THE TOP-POSTING already!

Sheesh man, please make some attempt to fit in here.
 
S

Sherm Pendley

Tad said:
STOP THE TOP-POSTING already!

What are you talking about? His quoting may have been a bit excessive,
but his reply was at the bottom. He didn't top-post. In fact, I can't
see that he's done any top-posting in this whole thread.

sherm--
 
J

Jürgen Exner

Thanks for your clarification on this. I went to the perl group
first because I was hoping that there was something in perl
that could be done outside of the html range of things
(i.e. fancy scripting tricks that are similiar to javascript
type page in place actions).

You may want to read up on the difference between client side scripting and
server side scripting. Because CGI is server- side scripting of course the
context and the capabilities are totally different from client-side
JavaScript.
Of course you could always use client-side PerlScript, then you get similar
capabilities as in JavaScript or VB Script.

Oh, and of course none of this has anything to do with the programming
language Perl, so please take the discussion to a more appropriate NG like
e.g. some with CGI or HTML in its name.

jue
 
T

Tad McClellan

Sherm Pendley said:
What are you talking about? His quoting may have been a bit excessive,
but his reply was at the bottom. He didn't top-post. In fact, I can't
see that he's done any top-posting in this whole thread.


Oops. Sorry.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top