Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Passing results to a script after form element validation

by Anonymous Monk
on Apr 14, 2004 at 10:45 UTC ( #344977=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Fellow Monks,

I have put some validation onto a form so to ensure that a field gets filled. This works fine for me, except now I want the form elements to get passed onto another cgi script. Here's a bit of the code

# Process form if submitted; otherwise display it if ($co->param("submit")){ process_form ( ); }else{ display_form ( ); } sub process_form{ if ( validate_form()){ print $co->start_form( -method=>'POST', -action=>"http://mywebserver/cgi-bin/script2.cgi +", ), $co->end_form(), $co->end_html; END{} } } sub validate_form{ my $searchfield = $co->param("searchfield"); my $error_message = ""; $error_message .= "Please enter a value into the searchfield<b +r>" if ( $searchfield eq "" ); if ($error_message){ # Errors with the form - redisplay it and return failu +re display_form ( $error_message, $searchfield, ); return 0; }else{ # Form OK - return success return 1; } }
The display_form sub simply displays the main page, starts like this
sub display_form{ my $error_message = shift; my $searchfield = shift; print $co->start_form( -method=>'POST', -action=>"http://mywebserver/cgi-bin/script1.cgi +", ), . . . #loads of extra stuff here . . $co->hidden( -name=>'submit', -value=> "Submit"), ), $co->end_form(),
Everything works fine, except the sub process_form, I would like this sub to send my fields onto script2.cgi for further processing (i.e. as if a hidden submit button on this form was automatically pressed - can this be done) can anybody direct me into the right direction for doing this.

Also, I was wondering, (and I really don't mean to offend anyone here) are the scripts used to create the perlmonks website available to take a look at or even download?

Thanks in advance,
Jonathan

Replies are listed 'Best First'.
Re: Passing results to a script after form element validation
by Anneq (Vicar) on Apr 14, 2004 at 12:44 UTC

    Not sure I understand what you are trying to do. But if I do, then perhaps you can put your scipt2.cgi into a sub inside a module, and then just call call the sub2, passing on the $co object.

    For example:

    sub process_form{
            if ( validate_form()){
                    $result = MyModule->mysub2($co);
            }
    }
    

    HTH

    Anne

      I agree you should do everything in the same cgi if possible. In some cases you can't do that, though, and maybe that's the case here. (For instance, you're using a 3rd party's cgi on their site.)

      Two ideas off the top of my head:

      1. do something funny with javascript
      2. use perl's LWP to do the post, grab the results (or part) and display to the user

      I recommend #2.

      Rucker

Re: Passing results to a script after form element validation
by Anonymous Monk on Apr 14, 2004 at 19:01 UTC
    1. Do the validation and processing within the same script. This would be my perferred choice. If validation fails, print an error and the form again. If the validation is successful, process the request and print a results page.

    2. Access script2.cgi HTTP GET rather than a POST. Just put your CGI variables in to a query string and append on to the end of the URL for the second script. Then put that URL into a meta refresh tag in the HTML header e.g.
      <HEAD> <META HTTP-EQUIV="REFRESH" CONTENT="0;url=script2.cgi?searchfield=valu +e"> </HEAD>
Re: Passing results to a script after form element validation
by Anonymous Monk on Apr 14, 2004 at 11:50 UTC
    I would like this sub to send my fields onto script2.cgi for further processing (i.e. as if a hidden submit button on this form was automatically pressed - can this be done) can anybody direct me into the right direction for doing this.
    CGI does not work like that.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://344977]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2020-10-22 22:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (230 votes). Check out past polls.

    Notices?