Solved

Simple Question

Posted on 2000-04-14
2
144 Views
Last Modified: 2013-12-25
how do i write a cgi that decodes entry from a form and then saves it to a file? (i don't have cgi.pm so is this possible?)
0
Comment
Question by:MegaX
  • 2
2 Comments
 

Accepted Solution

by:
venky75 earned 73 total points
ID: 2718450
Hi,

try this program out it.Doesnot require any .pm files for decoding the entries and also saves to file.

bye
venky

#!/usr/bin/perl

%in= &getcgivars ;

open(FILE,">>filename.txt");
print FILE "\n";
$maxlength= 0 ;
foreach (keys %in)
{
 $maxlength= length if length > $maxlength ;
}
$maxlength++ ;
foreach (sort keys %in)
{
 if ($in{$_}=~ /\n/)
 {
  $in{$_}= "\n" . $in{$_} ;
  $in{$_}=~ s/\n/\n    /g ;
  $in{$_}.= "\n" ;
 }
 $in{$_}=~ s/\0/, /g ;
 printf FILE "%-${maxlength}s  %s\n", "$_:", $in{$_} ;
}
close (BOOK);

sub getcgivars
{
 local($in, %in) ;
 local($name, $value) ;
 if ($ENV{'REQUEST_METHOD'} eq 'GET')
 {
  $in= $ENV{'QUERY_STRING'} ;
 }
 elsif ($ENV{'REQUEST_METHOD'} eq 'POST')
 {
  if ($ENV{'CONTENT_TYPE'}=~ m#^application/x-www-form-urlencoded$#i)
  {
   $ENV{'CONTENT_LENGTH'} || &HTMLdie("No Content-Length sent with the POST request.") ;
   read(STDIN, $in, $ENV{'CONTENT_LENGTH'}) ;
   }
   else
   {
    &HTMLdie("Unsupported Content-Type: $ENV{'CONTENT_TYPE'}") ;
   }
 }
 else
 {
  &HTMLdie("Script was called with unsupported REQUEST_METHOD.") ;
 }
   
 foreach (split('&', $in))
 {
  s/\+/ /g ;
  ($name, $value)= split('=', $_, 2) ;
  $name=~ s/%(..)/chr(hex($1))/ge ;
  $value=~ s/%(..)/chr(hex($1))/ge ;
  $in{$name}.= "\0" if defined($in{$name}) ;  
  $in{$name}.= $value ;
 }
 return %in ;
}

sub HTMLdie
{
 local($msg,$title)= @_ ;
 $title || ($title= "CGI Error") ;
 print <<EOF ;
 Content-type: text/html

 <html>
 <head>
 <title>$title</title>
 </head>
 <body>
 <h1>$title</h1>
 <h3>$msg</h3>
 </body>
 </html>
 EOF
 exit ;
}
0
 

Expert Comment

by:venky75
ID: 2718452
Hi there,
 sorry about the part where it is close(BOOK); instead it should be close(FILE);

Kindly change the same at ur end.

Bye
venky
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will aim to show you how simple is making a small application in WhizBase, how to add, remove and update data in the DB. I will make a small address book application where you can add, browse, update and remove addresses. I wi…
Making a simple AJAX shopping cart Couple years ago I made my first shopping cart, I used iframe and JavaScript, it was very good at that time, there were no sessions or AJAX, I used cookies on clients machine. Today we have more advanced techno…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now