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

Discussion in 'Perl Misc' started by dale zhang, Nov 23, 2004.

  1. dale zhang

    dale zhang Guest

    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
    dale zhang, Nov 23, 2004
    #1
    1. Advertising

  2. Re: how to define a variable to hold a multiline text input in perlfrom html multiline textbox

    (dale zhang) writes:
    > 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
    --
    Come to think of it, there are already a million monkeys on a million
    typewriters, and Usenet is NOTHING like Shakespeare.
    -- Blair Houghton.
    Eric Schwartz, Nov 23, 2004
    #2
    1. Advertising

  3. dale zhang <> wrote:


    > 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?


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Nov 24, 2004
    #3
  4. dale zhang

    dale zhang Guest

    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

    Eric Schwartz <> wrote in message news:<>...
    > (dale zhang) writes:
    > > 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
    dale zhang, Nov 29, 2004
    #4
  5. dale zhang

    Paul Lalli Guest

    "dale zhang" <> wrote in message
    news:...
    > Here is my script:
    >
    > #!/usr/bin/perl


    <snip>

    > 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
    Paul Lalli, Nov 29, 2004
    #5
  6. Re: how to define a variable to hold a multiline text input in perlfrom html multiline textbox

    dale zhang wrote:

    > #!/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--

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
    Sherm Pendley, Nov 29, 2004
    #6
  7. Re: how to define a variable to hold a multiline text input in perlfrom html multiline textbox

    (dale zhang) writes:
    > 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
    --
    Come to think of it, there are already a million monkeys on a million
    typewriters, and Usenet is NOTHING like Shakespeare.
    -- Blair Houghton.
    Eric Schwartz, Nov 29, 2004
    #7
  8. dale zhang

    dale zhang Guest

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

    Thanks a lot. -Dale

    Eric Schwartz <> wrote in message news:<>...
    > (dale zhang) writes:
    > > 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
    dale zhang, Nov 29, 2004
    #8
  9. dale zhang

    Tintin Guest

    "dale zhang" <> wrote in message
    news:...
    > Eric Schwartz <> wrote in message
    > news:<>...
    >> (dale zhang) writes:
    >> > 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.

    >
    > 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
    Tintin, Nov 30, 2004
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Anand P Paralkar

    Input Delay and Hold Time

    Anand P Paralkar, Jan 16, 2004, in forum: VHDL
    Replies:
    4
    Views:
    10,260
    Alexander Gnusin
    Jan 20, 2004
  2. Simon Middlemiss
    Replies:
    2
    Views:
    5,237
    Jacob Yang [MSFT]
    Nov 10, 2003
  3. Terry
    Replies:
    2
    Views:
    760
    Terry
    Feb 16, 2007
  4. Replies:
    3
    Views:
    748
    George Ter-Saakov
    Feb 23, 2007
  5. John Reye
    Replies:
    14
    Views:
    777
    Tim Rentsch
    May 8, 2012
Loading...

Share This Page