Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Simple Question

Posted on 2000-04-14
2
Medium Priority
?
153 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 

Accepted Solution

by:
venky75 earned 292 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
The viewer will learn how to dynamically set the form action using jQuery.

670 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