<FORM> Submit question with perl cgi scripts

Discussion in 'Perl Misc' started by dogdog@noemail.com, Dec 1, 2004.

  1. Guest

    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
    , Dec 1, 2004
    #1
    1. Advertising

  2. wrote:
    > 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.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Dec 1, 2004
    #2
    1. Advertising

  3. wrote:

    > 1. Have the response post back to my original webpage and
    > not create a seperate page with the information.


    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.

    > Any way to do this?


    All is revealed in the docs for CGI.pm:

    perldoc CGI

    or

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

    sherm--

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
    Sherm Pendley, Dec 1, 2004
    #3
  4. Guest

    On Wed, 01 Dec 2004 17:49:34 +0000, Sherm Pendley wrote:

    > wrote:
    >
    >> 1. Have the response post back to my original webpage and
    >> not create a seperate page with the information.

    >
    > 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.
    >
    >> Any way to do this?

    >
    > 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
    , Dec 1, 2004
    #4
  5. Guest

    On Wed, 01 Dec 2004 23:45:21 +0100, Gunnar Hjalmarsson wrote:

    > wrote:
    >> 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.

    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
    , Dec 1, 2004
    #5
  6. wrote:
    > Gunnar Hjalmarsson wrote:
    >> wrote:
    >>> 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.

    >
    > 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__

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Dec 2, 2004
    #6
  7. wrote in
    news:p:

    > 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?

    --
    A. Sinan Unur
    d
    (remove '.invalid' and reverse each component for email address)
    A. Sinan Unur, Dec 2, 2004
    #7
  8. wrote:

    > 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--

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
    Sherm Pendley, Dec 2, 2004
    #8
  9. Guest

    On Wed, 01 Dec 2004 20:40:56 +0000, Sherm Pendley wrote:

    > wrote:
    >
    >> 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--


    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
    , Dec 2, 2004
    #9
  10. Guest

    On Wed, 01 Dec 2004 16:16:35 +0000, Jim Gibson wrote:

    > In article <>,
    > <> wrote:
    >
    >> On Wed, 01 Dec 2004 23:45:21 +0100, Gunnar Hjalmarsson wrote:
    >>
    >> > wrote:
    >> >> 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.

    >> 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.

    >
    > 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
    , Dec 2, 2004
    #10
  11. Guest

    On Thu, 02 Dec 2004 01:26:46 +0100, Gunnar Hjalmarsson wrote:

    > wrote:
    >> Gunnar Hjalmarsson wrote:
    >>> wrote:
    >>>> 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.

    >>
    >> 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__

    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
    , Dec 2, 2004
    #11
  12. <> wrote:

    [ 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.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Dec 2, 2004
    #12
  13. Tad McClellan wrote:

    > 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--

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
    Sherm Pendley, Dec 2, 2004
    #13
  14. wrote:
    > 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
    Jürgen Exner, Dec 2, 2004
    #14
  15. Sherm Pendley <> wrote:
    > Tad McClellan wrote:
    >
    >> 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.



    Oops. Sorry.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Dec 2, 2004
    #15
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page