how to define a variable to hold a multiline text input in perl from html multiline textbox

D

dale zhang

Hi,

I have a perl script (run when "submit" is clicked) to read all
one-line text box inputs. However it has trouble to read a multiline
textbox (like user question input) to $question. Do I define the
variable wrongly?

Thanks a lot. -Dale
 
E

Eric Schwartz

I have a perl script (run when "submit" is clicked) to read all
one-line text box inputs. However it has trouble to read a multiline
textbox (like user question input) to $question. Do I define the
variable wrongly?

You should just be able to do:

my $multiline = param('multiline');

in your script. You are using CGI.pm (or CGI::Lite, or something like
that), right? If that doesn't do the trick, or you still don't
understand, please read the Posting Guidelines first, and then post a
small (10-20 lines is ideal), *COMPLETE* program that shows the
problem, along with a description of what is supposed to happen, and
what actually happens.

-=Eric
 
T

Tad McClellan

I have a perl script (run when "submit" is clicked)


Perl is not CGI.

CGI is not Perl.

Do I define the
variable wrongly?


Since you haven't shown us how you are defining the variable
we can't very well answer that question, now can we?
 
D

dale zhang

Thank you for your help. I am sorry I did not make my question clearly
at the first place. Our email system got some problems, so I have to
wait to send this out. Here is my script:

#!/usr/bin/perl

&populatePostFields;
$title = $postFields{ "select" };
$firstName = $postFields{ "textfield1" };
$lastName = $postFields{ "textfield2" };
$email = $postFields{ "textfield3" };
$dayPhone = $postFields{ "textfield4" };
$companyName = $postFields{ "textfield5" };
$webSite = $postFields{ "textfield6" };
$question = $postFields{ "textfield7" };
$bestTime = $postFields{ "select2" };
$timeZone = $postFields{ "select3" };
$hearSource = $postFields{ "select4" };
$otherSource = $postFields{ "textfield8" };

print "Content-Type: text/html\n\n";

print "Hello, World1!\n";
print "title $title, fname $firstName, lname $lastName, email
$email\n";
print "day# $dayPhone, comName $companyName, web $webSite\n";
#print "question length $string_length\n"
#print "question $question\n"
#print "bestTime $bestTime, timeZone $timeZone, hearSource
$hearSource, otherSource $otherSource\n";

sub populatePostFields {
%postFields = ();
read( STDIN, $tmpStr, $ENV{ "CONTENT_LENGTH" } );
@parts = split( /\&/, $tmpStr );
foreach $part (@parts) {
( $name, $value ) = split( /\=/, $part );
$value =~ ( s/%23/\#/g );
$value =~ ( s/%2F/\//g );
$postFields{ "$name" } = $value;
}
}

I can not print $question, so I comment it out. My guess is $question
is not properly assigned since the textfield7 has multiline (it is a
textarea in HTML).

Any suggestions? -Dale
 
P

Paul Lalli

dale zhang said:
Here is my script:

#!/usr/bin/perl

sub populatePostFields {
%postFields = ();
read( STDIN, $tmpStr, $ENV{ "CONTENT_LENGTH" } );
@parts = split( /\&/, $tmpStr );
foreach $part (@parts) {
( $name, $value ) = split( /\=/, $part );
$value =~ ( s/%23/\#/g );
$value =~ ( s/%2F/\//g );
$postFields{ "$name" } = $value;
}
}

Any suggestions? -Dale

Yes, get rid of this entirely. Use CGI.pm;. Also use strict; and use
warnings;. Read about CGI.pm (and how much easier this code suddenly
becomes once you use it) by typing
perldoc CGI

Paul Lalli
 
S

Sherm Pendley

dale said:
#!/usr/bin/perl

use strict;
use warnings;

use CGI qw(fatalsToBrowser);
&populatePostFields;

my $q = new CGI;
my %postFields = $q->Vars();
I can not print $question, so I comment it out. My guess is $question
is not properly assigned since the textfield7 has multiline (it is a
textarea in HTML).

Any suggestions? -Dale

Delete the buggy homemade populatePostFields subroutine and use one that
works properly.

Also, learn to call subroutines correctly. The '&' was needed for Perl
4, but in Perl 5 it's supported for compatibility only, and has side
effects you probably don't want. See 'perldoc perlsub'.

sherm--
 
E

Eric Schwartz

Thank you for your help. I am sorry I did not make my question clearly
at the first place. Our email system got some problems, so I have to
wait to send this out. Here is my script:

Thanks for posting this, though it might have been cut down quite a
bit to simply display the problem. Here follow my comments:
#!/usr/bin/perl

use warnings;
use strict;
&populatePostFields;

Why are you calling this function with an '&'? That hasn't been
necessary since Perl 4, at least. This should be:

populatePostFields();

-- except actually, you shouldn't use this at all. Just replace this with:

use CGI qw/:standard/;
$title = $postFields{ "select" };
$firstName = $postFields{ "textfield1" };
$lastName = $postFields{ "textfield2" };
$email = $postFields{ "textfield3" };
$dayPhone = $postFields{ "textfield4" };
$companyName = $postFields{ "textfield5" };
$webSite = $postFields{ "textfield6" };
$question = $postFields{ "textfield7" };
$bestTime = $postFields{ "select2" };
$timeZone = $postFields{ "select3" };
$hearSource = $postFields{ "select4" };
$otherSource = $postFields{ "textfield8" };

All those variables need to be declared, with 'my'. Read 'perldoc
strict' for more information on why.

FYI, those are terrible names in the HTML! If you want to make your
script and HTML easy to maintain, rename 'select' in your HTML source
to 'title', 'textfield1' to 'firstName', and so on. Then you don't
have to use all these temporary variables! Just do:

print "Title is: ",param('title'),"\n";

or something like that. It's much more readable than:

my $title = param('select');

for what I hope are obvious reasons.
print "Content-Type: text/html\n\n";

print header, start_html("Hello, World1!");
print "Hello, World1!\n";

Above you said you were sending text/html to the browser, but you're
not actually sending it. If you mean text/plain, then say so. If you
mean text/html, then print that.

To print a text/plain content header with CGI.pm, just use:

header('text/plain')

instead of

header

above.
print "title $title, fname $firstName, lname $lastName, email
$email\n";
print "day# $dayPhone, comName $companyName, web $webSite\n";
#print "question length $string_length\n"
#print "question $question\n"
#print "bestTime $bestTime, timeZone $timeZone, hearSource
$hearSource, otherSource $otherSource\n";

sub populatePostFields {
%postFields = ();

Don't declare package variables in a sub, especially when they're not
needed.
read( STDIN, $tmpStr, $ENV{ "CONTENT_LENGTH" } );

A) What's $tmpStr? If it's another package variable, then see above--
it's not necessary, and adds to the confusion.
B) The quotes around CONTENT_LENGTH are unnecessary
C) You never check the results of the read(), so you don't know how
many bytes you actually read.
@parts = split( /\&/, $tmpStr );

I know GET queries are allowed to separate parameters with ';' as well
as '&'; are POST ones as well?
foreach $part (@parts) {
( $name, $value ) = split( /\=/, $part );
$value =~ ( s/%23/\#/g );
$value =~ ( s/%2F/\//g );

Your decoding here is painfully incomplete. There's a lot more to URI
decoding than that.
$postFields{ "$name" } = $value;

Again, quotes around the hash key are unnecessary here.
}
}

I can not print $question, so I comment it out. My guess is $question
is not properly assigned since the textfield7 has multiline (it is a
textarea in HTML).

That's possible, but what do you mean by "I cannot print $question"?
Does the program die at that point, does it print out nothing, does it
send an error message to your server's error log that says, "Sorry,
you are allowed to print out anything except $question"? Specifics
help here-- what exactly did it do, what exactly did you expect it to?
Any suggestions? -Dale

Stop trying to parse CGI input yourself; it's a much more complicated
task than you evidently think it is. Use CGI.pm instead. It's a
standard module included with Perl, so you don't even have to search
for it or try to install it. Just use it.

Also, when you tell a browser you're going to send it text/html, and
then don't, it's quite possible something quirky is going on there,
but since you don't tell us what actually happens when you try to
print out the variable in question, that's only a wild guess.

I'm certain about using CGI.pm, though.

-=Eric
 
D

dale zhang

Please ignore my follow-up message that includes the script. I find a
way to handle multiline textarea now.

Thanks a lot. -Dale
 
T

Tintin

dale zhang said:
Please ignore my follow-up message that includes the script. I find a
way to handle multiline textarea now.

Hopefully that "way" involved using CGI.pm
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,269
Messages
2,571,099
Members
48,773
Latest member
Kaybee

Latest Threads

Top