Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 193
  • Last Modified:

Form Input

I have looked at many 'canned' perl scripts, however these scripts always have way too much coded, which makes me confused on what --REALLY-- needs to be in the code for a function to happen.  So please give me bare bones code (code that does only what I specify)

THE PROBLEM:
I need to be able to read the input submitted from an HTML form ( I do not know wheather to send it POST or GET ).  After the program has read the input, it must parse it and separate it into it's values for easy access with a hash.  The program also must convert any hex codes (i.e. %20 = ' '(space)) and it must parse out or correctly replace the & signs and + signs etc..
EXAMPLE:
submitted input from a form...
a.cgi?name=joe+anderson&occupation=Lawer&action_code=1&email=yes
-=---->>
with the input from above, the program should store everything to a hash so that I can use it. like...
%information{
        'Names'=> 'joe anderson'
        'occupation => 'Lawer'
        'action_code' => '1'
        etc......
hope you can help ........
thanks in anvance

0
winwiz
Asked:
winwiz
  • 6
  • 5
  • 2
  • +2
1 Solution
 
ozoCommented:
#!/usr/bin/perl
use CGI qw(:standard);
for( param ){ $information{$_} = param($_) }
0
 
jyu_88Commented:
all input values is stored in a hash by the CGI module (when you use 'new' to create a new instance of CGI object), you can retrive them just like this:
-------cut-------cut----
#!/usr/bin/perl
use CGI qw(:standard);
$query = new CGI;
# assume 'foo' is a input field name
$valueOfFoo = $query->param('foo');
exit 0;
----cut--------cut---
in other words, you don't really need to have your own hash to store the inputs
0
 
jyu_88Commented:
BTW, since you are sort of concerned with the METHOD, CGI module does not care it has POST or GET, it will take care of that internal to the module and totally transparent to you.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
maneshrCommented:
try this...

this is a very generic sub routine, with no hardcoded values. you can use this with ANY PERL script.

the final values are store in a hash (%information in this case).
you need to use it throughout your program.

=======

#!/usr/local/bin/perl

use CGI;
use URI::Escape;

## Read the form variable names and values into local variables.
$query=new CGI;

## This is the subroutine that does the
## Actual read and decodes the vars too!
%information=&read_form_vars();

sub read_form_vars{
  foreach($query->param){
    ## Escape the variables to remove any "unsafe" values
    $str  = uri_unescape($query->param($_));

    ## Store the variables in a hash.
    ## The keys of the has are the varibale names and the values for those keys are the values that the user has entered in the form.
    $information{$_}=$str;
  }  

  ## Return the final hash.
  return (%information);
}
0
 
moonlxCommented:
#use the CGI class
use CGI;

my $query = new CGI;

#'name' for example is the name of the input box in your form, also use the POST method

%information{
        'Names'=> $query->param('name')
        'occupation => $query->param('occupation')
        'action_code' => $query->param('action_code')
        etc......


0
 
winwizAuthor Commented:
none of these methods seem to work correctly.
Do i need to put CGI.pm in the same directory as the cgi script?

Also... I need to know how to delete multiple items from an array at once... You see, I want the user to view a database and place a check next to the item they want deleted, however they may check many items.  How can I make sure the correct entries are deleted?  I treid a loop, but because the index changes after each removal, I do not know what to do.
Thanks

Jason

P.S.  I boosted the points to 100 for the extra help.
:)
0
 
winwizAuthor Commented:
Adjusted points from 100 to 150
0
 
winwizAuthor Commented:
oop, i mean 150 pts.
:)
0
 
ozoCommented:
You need to put CGI.pm in in one of your @INC directories.
What error are you getting?
You can delete multiple items from an array at once using splice
perldoc -f splice
0
 
winwizAuthor Commented:
i got it working now.
however, you must show code of the splice... I know how to remove 1 at a time with splice.... please use an example of how to delete indexes 1 and 2 of @cars in one line of code.
thx
0
 
ozoCommented:
splice @cars,1,2; #that may be a confusing example.  2 is the length, not a second index
0
 
winwizAuthor Commented:
but i need to be able to remove indexes like 2,5,8,21 all in one statement?
how?
0
 
ozoCommented:
@cars = @cars[0..1,3..4,6..7,9..20,22..$#cars];
0
 
winwizAuthor Commented:
i accepted this answer, because it was the one that worked best for me.  And ozo... you obvously don't get my situation.
0
 
ozoCommented:
#true, I don't get why it needs to be all in one statement
#and depending on what you mean by remove, it could be
@cars[2,5,8,21] = ();
#or
@cars=@cars[grep{!${{map{$_,1}2,5,8,21}}{$_}}0..$#cars];
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 6
  • 5
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now