Click to See Complete Forum and Search --> : Perl & CGI
Eff-Three
02-23-2001, 07:22 AM
Hello. If I have a cgi script called script.pl and I have a subroutine in that script called subroute then how can I access that certain subroutine through the browser?
I have seen other cgi scripts do it like this http://www.cgiscriptpage.com/cgi-bin/script.pl?action=subfunc
How do I enable the possibility to do that in my cgi scripts? I've tried just regularly accessing the function through the web browser like it is done in the kind of urls gives above but it didnt work.
How can I do it?
Thanks, F3
Eff-Three
02-23-2001, 08:43 AM
It doesn't matter I found out how to do it but I have one more question. If I have a cgi script that prints a form then I want the user to enter values in the form then for the form action where would I make it submit to? And how can I get the values? I see how it works from an html form to a script but from a script to itself I don't get. How is this done?
Thank you
F3
YaRness
02-23-2001, 09:03 AM
in general terms, you can parse the command line parameters and look for something like 'action=subroute' or whatever. if the script uses CGI.pm, then read up on CGI.pm, there are functions that make paired command line parameters like that easy.
Eff-Three
02-23-2001, 09:27 AM
Yeah thanks I found that out, but I still cant figure out how to do my other question. If I have a cgi script that is used to generate a form and then get values then when it prints the form html, where do I make it post the results to? Is there a way to get form results entered in a form when the form is generated by the script itself?
YaRness
02-23-2001, 09:45 AM
i think a lot of simple scripts are designed to post a form, and then pass the results to itself. the general setup is, if the script is called with no paramters (i.e., http://www..../script.pl ), then it returns the web page with the form. the action for the form is the script itself, so when you hit the submit button, it calls the script with the form elements in the parameters (i.e. http://www..../script.pl?something=foo&bar=baz ). when the script gets called with paramters, instead of posting the form, it does whatever it needs to do with the parameters, and posts the results.
Eff-Three
02-23-2001, 10:02 AM
Ah I think I've been looking at it all wrong. Thanks I think I now have an idea of how I should go about doing this.
F3
Morph
03-01-2001, 11:37 AM
you seem to be asking some very basic questions. I was in the same situationbut then I foudn a great book from a cetain online book store ;) that saved my life. Online help I foudn to be quite useless. This book is from Cookwood press. Just go to www.cookwood.com (http://www.cookwood.com) . I had some general problems and emailed her - she worte back to me personally. Now thats what I call support! But now she has a very useful bulletin board on her site too. But buy the book, its well worth it (I get NO money from saying all this and am in no way affiliated with her!!) - its just a god damn good book! :D :D
:D
You may want to consider reading up on the HTTP protocol a bit. Any good CGI book should have at least an introduction to it, so you may want to start there. Understanding the HTTP Request/Response process will help you with the questions you just asked.
As far as getting values from a form generated by CGI script. You do it the same way you would do it from an HTML-based form. If you're using the CGI.pm module you can use the 'param' function. Check into that.
Fandelem
03-01-2001, 06:08 PM
Originally posted by YaRness:
i think a lot of simple scripts are designed to post a form, and then pass the results to itself. the general setup is, if the script is called with no paramters (i.e., http://www..../script.pl ), then it returns the web page with the form. the action for the form is the script itself, so when you hit the submit button, it calls the script with the form elements in the parameters (i.e. http://www..../script.pl?something=foo&bar=baz ). when the script gets called with paramters, instead of posting the form, it does whatever it needs to do with the parameters, and posts the results.
hey yar, how do you do the continual loop whether or not "parameters" have been passed or not? i've always had a submit.html file that calls submit.pl and it's worked fine; however i'd like to have it just call submit.pl for everything.. but i haven't figured out how to continually loop my form() function (that displays the submit.html basically) until the user inputs all the fields and/or hits the submit button (ie how can i get the script to wait until it has been submitted to continue on with the rest of the function calls?)
YaRness
03-02-2001, 08:22 AM
lemme try and illustrate with pseudocode for script.pl
script.pl
if (<there are parameters> )
{
#here is where the action happens
#where we parse and do something with
#the information the user entered in
#the form
}
elsif (<there are NO parameters> )
{
#here we just have a bunch of output
#statements that build our web page
#with a form in it, and the action
#part of the form is this same script
}
you don't need a loop. when the user requests the script.pl url the first time (ie foo.com/script.pl), all it does is spit out your form page (i.e. the same thing as the 'submit.html' you mentioned). when the user submits the form (ie foo.com/script.pl?foo=bar) all the script does is process the parameters and return whatever resulting information, redirection, ad nauseum it's supposed to do.
there's lot of stuff like cookies and whatnot on both client and server side to retain state, but for the most part http stuff still retains it's original design: user requests a page, server sends it, and that's it. the server doesn't need to maintain any running process waiting for the user to do something else; that's taken care of by having the user indicate what they wanna do next with parameters passed into the script and switch statements (if/elsif) deciding where to point the user based on parameters they submitted.
my $submit = $q->param('submit'); # binds the input from the 'submit' parameter
if ( !( $submit ) ) { # checks to see if $submit is undefined
# empty form would be output here
} elsif ( $submit ) {
# check and process your parameters here
}
This sample assumes that you are using CGI.pm(which you should be :)).
Hope that helps.
[ 03 March 2001: Message edited by: kel ]
Fandelem
03-03-2001, 03:52 AM
for some reason it never actually continues withmy script. here is what i've done:
my $cgi = new CGI;
my $submit = $cgi->param('submit'); # binds the input from the 'submit' parameter
&submitform;
if ( !( $submit ) ) {
#&submitform;
print "empty form! try again!!";
exit;
}
elsif ( $submit ) { # now we continue with the rest of the script.
# print the date
&get_date;
# print html redirection stuff
&redirect;
# sort everything submitted.
&getcgi;
# send mail to admin
&send_mail;
# let's store everything in a file.
&logpoem;
# print footer
&footer;
}
this is just a snippet, i hope it's enough to figure out what i'm doing wrong though..
In the branch of the first condition:
if ( !( $submit ) ){
}
The check is really just to see if the user has been to the page yet. You would execute code for the empty form the user can fill out there. You could check the form input in another branch where submit is defined but other parameters are not.
If you need a better sample, let me know and I'll post one or email it to you.