Solved

Form Input

Posted on 2000-03-25
15
183 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 84

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 150 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
 
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 2

Author Comment

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

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 84

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 84

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 84

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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

744 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

11 Experts available now in Live!

Get 1:1 Help Now