Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Calling one cgi script from within another

Posted by astradyne (astradyne), 22 March 2003
I have an HTML form with a number of fields on that calls a third party cgi (eg. process.cgi for processing.

However, I would like the form to call a different cgi (eg. validate.cgi so that I can validate the contents of the fields and then have this cgi call process.cgi if everything is okay.

I have three questions:

1) How do I go about extracting the input fields from the form in validate.cgi.  In routines I've looked at I see references to $fieldname=$input('name').

2) If I extract the input fields from the form into validate.cgi, will they still be available for extraction from process.cgi or will they have to be passed to the process.cgi routines in a different way?

3) How would I call the process.cgi from validate.cgi ?

I have done numerous searches on the web to try and answer these questions, but haven't found anything to guide me so far.

Thanks in advance for any help

Posted by admin (Graham Ellis), 22 March 2003
Welcome, Astradyne - you titled this "Really Silly Question" which it's not - it's excellent and merits quite some consideration.  I'm changing the title (which is really an advert for your post) so that others are encouraged to read / join in if they wish.

Posted by admin (Graham Ellis), 22 March 2003
Some initial answers:

1. cgi (The common gateway interface) passes information in to your script via the environment variable $ENV{QUERY_STRING} by default, or on STDIN if your for uses the POST method. You're posring to the Perl board here, so I think you want to use Perl to perorm your task ... you might want to use the language directly to perform the task, or you may prefer to use a module like CGI.pm.   If you do it directly, here's the sort of code that you might use:

Code:
if ($ENV{"REQUEST_METHOD"} eq "POST") {
       read(STDIN,$buffer,$ENV{"CONTENT_LENGTH"});
       $form{"method"} = "POST";
} else {
       $buffer = $ENV{QUERY_STRING};
       $form{"method"} = "GET";
}      

@fof = split(/&/,$buffer);
foreach $field(@fof) {
       ($name,$value) = split(/=/,$field);
       $value =~ tr/+/ /;
       $value =~ s/%([a-fA-F0-9]{2})/pack("C",hex($1))/eg;
       if ($form{$name}) {
               $form{$name} .= "\n$value";
       } else {
               $form{$name} = $value;
               }
       }


2. The embedded script will probably have the environment information available to it (but it does depend on how you next your cgi scripts), but any inputs on STDIN will be consumed by the validator and you'll need to pass them on in some way.

3. If it's perl calling perl, a require is the easiest way.

Here's a complete working example that I put together on my test server.   Firstly, the form:

Code:
<body>
<form action=checkit.cgi>
Please enter a whole number <input name=what><br>
and <input type=submit>
</form>
</body>


The validation script:

Code:
#!/usr/bin/perl

if ($ENV{QUERY_STRING} =~ /=\d+$/) {
       require "doit.cgi";
} else {
       print ("Content-type: text/html\n\n");
       print ("Validation failed - expecting a series of digits");
}


And the script that does the real work:

Code:
#!/usr/bin/perl

print ("Content-type: text/html\n\n");

($val) = ($ENV{QUERY_STRING} =~ /=(.*)/);
$twice = $val * 2;

print <<"AAA"
<body bgcolor=white>
<h1>Hello</h1>
I received $ENV{QUERY_STRING} so the entered value was $val<br>
Twice that value is $twice
</body>
AAA


Please be aware that my example makes it look much simpler than it may turn out to be in a real life situation, and that you've got a lot of learning to do - you haven't set yourself a beginner's exercise here.  However - depending on how you work there's a lot of help available in books, on courses, or on the net.


Posted by astradyne (astradyne), 22 March 2003
Hi Graham

Thanks for that, thats a great help.  The form I'm using uses the POST method to pass the parameters.  I coded the validation cgi for testing and using require the parameters are still retained for the second cgi which is just what I wanted.

A follow up question that I have, if I may, is this.  Is it possible to update the contents of the STDIN buffer based on the validation checks.

Using the code in your examples, I did this:

Code:
#! c:\perl\bin\perl.exe

if ($env{"REQUEST_METHOD"} eq "POST") {
  read(STDIN,$buffer,$ENV{"CONTENT_LENGTH"} ) ;
  $form{"method"} = "POST";
} else {
  $buffer = $ENV{QUERY_STRING} ;
  $form{"method"} = "GET";
}

@fof = split(/&/,$buffer) ;
foreach $field(@fof) {
  ($name,$value) = split(/=/,$field);
  $value =~ tr/+/ /;
  $value =~ s/%([a-fA-F0-0]{2})/pack("C",hex($1))/eg;
  if ($form{$name}) {
     $form{$name} .= "\n$value";
  } else {
     $form{$name} = $value;
  }
}

if ($form{'Subject'} eq "") {
  $form{'Subject'} = "Default Subject";      
}

require "posting.cgi";


The intention being that if the Subject field was left black then a default could be put in.  Needless to say this didn't work.  Is there any other way of achieving the same thing ?

Thanks for the help

Best regards

Jonathan


Posted by admin (Graham Ellis), 23 March 2003
I don't think it's going to be easy up update the content of STDIN, and as you've found changing the hash into which you've interpretted your input in the wrapper won't do much for you.

Environment variables changed in a Perl program DO effect other code that they call - so you could change GET data, and you could change the length of the post data.   And I'm taking it that it's a big "no-no" to alter the posting.cgi script.

Idea. STDIN is a global file handle.  So ... open a temporary file, write to it the string you WANT to be on STDIN, change the environement variable REQUEST_LENGTH to the file length, re-open the file of the data you want on STDIN.    ........ Ok - the purists will thrown their hands up in horror at this little ruse, but Perl is the PRACTICAL extraction and reporting language after all  

Posted by astradyne (astradyne), 23 March 2003
Hi Graham

In theory I could change the Posting.cgi, but I want that to be as a last resort.  If a newer version comes out, with functionality that I would like to use then I don't want to worry about transferring changes between releases.  Ultimately I may end up having to do that, but I'd prefer to explore other avenues first.

Interestingly enough though, after doing some research on the net and through some Perl books I came across the Param method.  The examples said that you could use this to retrieve the form parameters, and also to set them.

I added the following bit of code to the validate.cgi just before the require:

Code:
$Subject = "Test Subject"

use CGI param;
$query = CGI::new();
$query->param("Subject" , $Subject);


This time when the require was processed the contents of STDIN were lost.  I'm probably misinterpreting the concept behind the param method, but thought I'd share that.

All the best

Jonathan



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho