La12 has asked for the wisdom of the Perl Monks concerning the following question:

Hi everyone,

Here is my problem.

Mary needs to upload state abbreviations into a MySql Database. She will do it via a cgi form. I would like to use "post" and DBI to accomplish this. I must use only one texarea within a form. In other words, my form cannot look like this:

State1: State2: State3: State4: ...
It needs be like this:
States: __ |NY| |NJ| |CT| |ME| |TX| |FL| | | | | |__|

Now, here is the question...
Can I have only one textarea (2columns 52rows) to accept all the states that she wishes to input and then parse that data?
If so, what is the end of line character after each state? Would I get this; states=NYNJCTMETXFL or would I get; states=NY\nNJ\nCT\nME\nTX\FL ? Does CGI.pm "magically" take care of this problem for me? If you're driving in space at the speed of light and you turn your lights on would they work? -- Steven Wright joke

Thanks for your time.

Replies are listed 'Best First'.
Re: Entering data into MySql using CGI
by tachyon (Chancellor) on Oct 09, 2001 at 01:26 UTC

    I totally agree with cLive ;-). Suck it and see often works well. However why not allow some flexibility:

    $,="~"; $data = join'', <DATA>; @bits = split /\W+/, $data; print @bits; __DATA__ aa bb cc dd,ee,ff, gg hh ii jj kk ll mm nn oo pp|qq|rr ^&$just#$@another#%%^perl@!^hacker!#

    This will split your data on *any* non alphanumeric separators - newlines, spaces, commas, whatever.

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Entering data into MySql using CGI
by arturo (Vicar) on Oct 09, 2001 at 01:17 UTC
    Can I have only one textarea (2columns 52rows) to accept all the states that she wishes to input and then parse that data?

    OK, I wanted to just say "yes," because this really smells of homework (the restriction on the UI, for instance). But given that you *did* pose a specific question ...

    CGI.pm isn't really the controlling factor. It depends on how the *user* enters it -- do they put line breaks or other spacers in themselves, or do they type it all in without spacers of any sort? So my advice is, in the HTML that presents the form with the text input field (be it an input field or a textarea), you tell the users what you want, and have your program proceed according to that. Visually easiest on you and on your users, I would think, is a comma or a space. Then, you just use split on the resulting string that you get using CGI.pm's param function. split is wicked-easy to use:

    my $string="these are the times that try men's souls"; my @words = split /\s/, $string; # @words now holds "these", "are", "the" ... etc.

    HTH

    perl -e 'print "How sweet does a rose smell? "; chomp ($n = <STDIN>); +$rose = "smells sweet to degree $n"; *other_name = *rose; print "$oth +er_name\n"'
      Since the state abbreviations are only two characters long, you can write a regex that should handle it with no problems.
      $states = "NY\nNJWA\nALCOOR"; print "-$1-\n" while($states =~ /(\w\w)/g);
      It seems an odd way to do it, though. Why would the user want to type in each state that way? Seems much, much easier to go with someone else's suggestion here and use a SELECT box instead.

      (And headlights would work at the speed of light, since they're traveling relative to your speed.)

        "And headlights would work at the speed of light.."

        Hmm, methinks you need to read up on physics a little bit.

        Or think about this. If you could* travel at the speed of light and you turned them on, are you saying the light would shine forwards and then reflect back to you? If so, then light is travelling faster than the speed of light. Oh but hang on, it can't, coz it travels at the speed of light. Therefore, you travel through where you want them to shine at the same speed that the light shines at, so the lights won't illuminate things in front of you, therefore they don't "work".

        cLive ;-)

        *...let's forget about the time travel and infinite mass problems etc here for simplicity's sake!

Re: Entering data into MySql using CGI
by cLive ;-) (Prior) on Oct 09, 2001 at 01:10 UTC
    "...then I don't want to work for you" - Steven Wright.

    Why ask this question, when in the time it takes you to draw the pretty table, you could have tried this out for yourself and found out!

    cLive ;-)

    PS - I'd make it 3 chars across anyway, so she can use space instead of CR and then just split on "\s+" (whitespace).

Re: Entering data into MySql using CGI (boo)
by boo_radley (Parson) on Oct 09, 2001 at 01:21 UTC
    Mary needs to upload state abbreviations into a MySql Database. <deletia> I must use only one texarea within a form.

    What is the impetus for this requirement? It seems like using a good HTML  <select multiple> would be a better way to go.