Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Form Input

Posted on 2000-03-25
15
Medium Priority
?
192 Views
Last Modified: 2010-03-05
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
Comment
Question by:winwiz
  • 6
  • 5
  • 2
  • +2
15 Comments
 
LVL 85

Expert Comment

by:ozo
ID: 2657880
#!/usr/bin/perl
use CGI qw(:standard);
for( param ){ $information{$_} = param($_) }
0
 
LVL 3

Accepted Solution

by:
jyu_88 earned 300 total points
ID: 2658620
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
 
LVL 3

Expert Comment

by:jyu_88
ID: 2658622
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Expert Comment

by:maneshr
ID: 2658883
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
 

Expert Comment

by:moonlx
ID: 2664633
#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
 
LVL 2

Author Comment

by:winwiz
ID: 2674669
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
 
LVL 2

Author Comment

by:winwiz
ID: 2674671
Adjusted points from 100 to 150
0
 
LVL 2

Author Comment

by:winwiz
ID: 2674672
oop, i mean 150 pts.
:)
0
 
LVL 85

Expert Comment

by:ozo
ID: 2674875
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
 
LVL 2

Author Comment

by:winwiz
ID: 2675424
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
 
LVL 85

Expert Comment

by:ozo
ID: 2675442
splice @cars,1,2; #that may be a confusing example.  2 is the length, not a second index
0
 
LVL 2

Author Comment

by:winwiz
ID: 2676141
but i need to be able to remove indexes like 2,5,8,21 all in one statement?
how?
0
 
LVL 85

Expert Comment

by:ozo
ID: 2676275
@cars = @cars[0..1,3..4,6..7,9..20,22..$#cars];
0
 
LVL 2

Author Comment

by:winwiz
ID: 2677926
i accepted this answer, because it was the one that worked best for me.  And ozo... you obvously don't get my situation.
0
 
LVL 85

Expert Comment

by:ozo
ID: 2678005
#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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

927 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