CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Passing Data from HTML to PERL

 



Huffy
New User

Jul 3, 2003, 5:26 AM

Post #1 of 18 (2264 views)
Passing Data from HTML to PERL Can't Post

Hi all,

I'm sorry to sound like a complete novice, but there is somethig really basic I can't figure out and can't find anywhere. I have a number of pages that use the same cgi script, all the form fields are named the same, the only difference is a hidden marketing code which takes the form of a number. This is set as a value in my HTML page, and I want to pass it to a scalar variable in the cgi script.

Everything so far tells me how to pass the value of a form field entry but not just a straight forward simple variable value. Do I just use the same variable name in the script, I'm not sure.

Please could someone help.

Thanks

Huffy


fox
Novice

Jul 3, 2003, 6:54 AM

Post #2 of 18 (2262 views)
Re: [Huffy] Passing Data from HTML to PERL [In reply to] Can't Post

  
Start your cgi with:


sub parse_form {
my (@pairs, %in);
my ($buffer, $pair, $name, $value);

if ($ENV{'REQUEST_METHOD'} eq 'GET') {
@pairs = split(/&/, $ENV{'QUERY_STRING'});
}
elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
}
else {
print "You cant run this script from telnet/shell";
exit;
}

PAIR: foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);

$name =~ tr/+/ /;
$name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

($value eq "---") and next PAIR;
exists $in{$name} ? ($in{$name} .= "~~$value") : ($in{$name} = $value);
}
return %in;
}


%in =&parse_form;

And to access any form field passed to the script.

Use $in{"fieldName"}


(This post was edited by fox on Jul 3, 2003, 6:28 PM)


Huffy
New User

Jul 3, 2003, 7:16 AM

Post #3 of 18 (2258 views)
Re: [Huffy] Passing Data from HTML to PERL [In reply to] Can't Post

I think you may have missed the simplicity of the request!

Lets look at my real life example, i have a variable called marketingcode in my web page that is set to the value 1. I merely want to pass this to a variable called $marketing code in the cgi. So in simple terms, what goes after the = sign in the following line:

$marketingcode =

Thanks

Huffy


fox
Novice

Jul 3, 2003, 12:28 PM

Post #4 of 18 (2253 views)
Re: [Huffy] Passing Data from HTML to PERL [In reply to] Can't Post

 
You need

$marketingcode = $in{"marketingcode"};

In perl you can't just read the values of a form, you have to parse the information sent to your script from the browser. The sub routine I posted before does just that.

%in = &parse_form;

is the key line, it reads the output of the form parse sub routine into the hash %in.

Giving you $in{"marketingcode"} the value of what was typed into the form field, or the hidden pre-determined value.

If you have a form with 10 fields, each field name will be put into the hash %in. Which is why I said $in{"field_name"}

You could individually assign each field to a scalar but it is not nessecary when it's already being assigned as a key in %in.

This really makes form reading very simple.

Just put the sub routine in your perl script, and put the %in=&parse_form; statement wherever you want.

Then put in

print $in{"any_form_field_name"};

To test it out, if you play around with it a little you'll get really familiar with it and understand all that I'm saying.

Also, you could use CGI.pm to read form values, but I don't like CGI.pm. The sub routine is better.

Hope this clears up what I meant before


Paul
Enthusiast

Jul 3, 2003, 4:34 PM

Post #5 of 18 (2249 views)
Re: [fox] Passing Data from HTML to PERL [In reply to] Can't Post


Quote
Start you cgi with:


Ewww no, no don't use that awful code.


fox
Novice

Jul 3, 2003, 6:30 PM

Post #6 of 18 (2245 views)
Re: [Paul] Passing Data from HTML to PERL [In reply to] Can't Post

 
You spelled your wrong,

And you should never mis-quote people

hee hee Sly


Jasmine
Administrator / Moderator

Jul 3, 2003, 11:28 PM

Post #7 of 18 (2241 views)
Re: [fox] Passing Data from HTML to PERL [In reply to] Can't Post

That code isn't really recommended for actual use. Please use this instead:


Code
use CGI; 

my $q = CGI->new();

# then any variable is available with
# $q->param( 'fieldname' )



fox
Novice

Jul 4, 2003, 12:35 AM

Post #8 of 18 (2236 views)
Re: [Jasmine] Passing Data from HTML to PERL [In reply to] Can't Post


Quote
That code isn't really recommended for actual use. Please use this instead:


I would like to you explain why and what you mean by "acutal use"


davorg
Thaumaturge / Moderator

Jul 4, 2003, 1:49 AM

Post #9 of 18 (2233 views)
Re: [fox] Passing Data from HTML to PERL [In reply to] Can't Post


In Reply To

Quote
That code isn't really recommended for actual use. Please use this instead:


I would like to you explain why and what you mean by "acutal use"


The code that you used is typical of the code that was seen in broken CGI programs years ago before the CGI module was included with every installation of Perl. If you want a debate on why your alternative is a bad idea then take a look at this discussion over on Perlmonks, but in summary why would you possibly want to type all that code into every program if you can just use the existing code in CGI.pm?

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


davorg
Thaumaturge / Moderator

Jul 4, 2003, 1:50 AM

Post #10 of 18 (2232 views)
Re: [Huffy] Passing Data from HTML to PERL [In reply to] Can't Post

You just need to put a hidden field on your form that contains your marketing code.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


Huffy
New User

Jul 4, 2003, 2:04 AM

Post #11 of 18 (2231 views)
Re: [davorg] Passing Data from HTML to PERL [In reply to] Can't Post

Thanks Davorg, That is a very simple solution and works perfectly, it does exactly what i wanted it too. May be it's HTML I should be brushing up on rather than PERL!

Thanks again for your help.

Huffy


Paul
Enthusiast

Jul 4, 2003, 2:50 AM

Post #12 of 18 (2231 views)
Re: [fox] Passing Data from HTML to PERL [In reply to] Can't Post

Unfortunately the edit log spoils your "witty" response...


Quote
(This post was edited by fox on Jul 3, 2003, 9:28 PM)



fox
Novice

Jul 4, 2003, 1:08 PM

Post #13 of 18 (2221 views)
Re: [Paul] Passing Data from HTML to PERL [In reply to] Can't Post

 
I know, It was a joke!

I don't prefer CGI.pm out of preference is all. But I refuse to waste anymore time on this argument because people are suppose to be working together on this site in the common cause for better code. I simply don't appreciate the fashion that Jasmine went about correcting me without even giving a simple explanation, any references, and even addressing me. I don't like being "talk around", it's only fair that If I made a mistake, I be told directly and the reason why. I felt Jasmine was taking the sarcastic approach.

And as the owner of this site it's expected that you lead the example on how people can work together to learn.

I initially left this site after 2 years of use as a moderator because I was sick of people correcting me in this exact fashion. Obviously since I have left your still going about it the same way, and I simply am making a point of it rather than not using this site anymore because I like the purpose of this site. Unless my first approach of not using the site is better.

I don't know you tell me?
PS. I just read the initial part of the article you sent. And perhaps I could address some confusion in the reason I use my own code rather than CGI.pm. Firstly, I can upload files with my code, I posted code that I do not use anyway! But that's because I wanted to keep things simple. My code makes it so extremely easy to upload files that I would never ever ever go back to using CGI.pm. And the articles points on why not to use CGI.pm over my own has been addressed in my code. I just won't post my code because it's complicated to someone just learning. I don't know, I just thought I was keeping things simple!


Paul
Enthusiast

Jul 4, 2003, 2:13 PM

Post #14 of 18 (2220 views)
Re: [fox] Passing Data from HTML to PERL [In reply to] Can't Post


Quote
I just won't post my code because it's complicated to someone just learning.


Hmm no doubt overly complicated and buggy/insecure like the example above.

Btw, that is not *your* code.


fox
Novice

Jul 4, 2003, 2:19 PM

Post #15 of 18 (2217 views)
Re: [Paul] Passing Data from HTML to PERL [In reply to] Can't Post

 
I said I wouldn't post MY CODE yes I did, so I posted that Code I had instead, As you can see that code can not upload files the way I explained my code can, so I didn't say it was my code.

What is with you all, taking pieces of peoples statements and making sneered remarks,

That is why I stopped posting here in the first place, obviously that what you want me to do even still.

After being gone for a few years I thought something would have improved but it's the same old story.


Paul
Enthusiast

Jul 4, 2003, 2:21 PM

Post #16 of 18 (2216 views)
Re: [fox] Passing Data from HTML to PERL [In reply to] Can't Post

I'm sorry you posted some crappy code and it got picked up on.

It's not my fault.


Jane
Novice

Jul 6, 2003, 11:22 AM

Post #17 of 18 (2206 views)
Re: [fox] Passing Data from HTML to PERL [In reply to] Can't Post

Fox.

I think you're a downright cad. You should apologise to Jasmine immediately if you wish to retrieve any respect you might have had.

I believe Jasmine implicitly recommended that your code should only be used for training purposes so that beginners may get a basic idea of what happens to cgi input. It should not be used 'live'. I did not detect any sarcasm so you must be paranoid.

Regarding uploads...well...how simple do you want it? I can't see a simpler way until perhaps the first version of 'telepathic_upload.cgi' is released.

Jane.
Think |negatively :(|
map{$,--%(5>>1)^0?push@y,$_:unshift@y,$_}sort(Hacker=>another=>Perl=>Just);for(1&1=>1|0){push@y,shift@y}print"@y";


fox
Novice

Jul 6, 2003, 4:21 PM

Post #18 of 18 (2201 views)
Re: [Jane] Passing Data from HTML to PERL [In reply to] Can't Post

 
I apologize to everyone.

Sorry,

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives