0

The code below takes the name field entered in a HTML form and outputs " hello [entered name] how are you. Also in the form there is a selection for gender either male or female. I would like the CGI script to change background color to green color if chosen male and yellow color if chosen female. Right now the color is static(pink). I've been trying to get it to work but no luck

 #!/usr/bin/perl
 use CGI::Carp qw(fatalsToBrowser);

 component parts


    $qstring = $ENV{'QUERY_STRING'};
    @pairs = split(/&/, $qstring);
    foreach $pair (@pairs) {
            ($name, $value) = split(/=/, $pair);
            $value =~ tr/+/ /;
            $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
            $FORM{$name} = $value;
    }

  &thank_you;
   #}

  sub thank_you {

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

  print <<EndStart;

    <html>




   <body bgcolor="#ff69b4" value="F" > 

    <hr>

  EndStart

      print "<p>hello</p>\n"; print "<blockquote><em>$FORM{name} how are  you </em></blockquote>\n\n";

    print <<EndHTML;

    </body>
    </html>

   EndHTML

   exit(0);

Part of the HTML code

What is your gender? <input type="radio" name="gen1" value="M"> male <br>
              <input type="radio" name="gen2"  value="F"> female <br>

 name: <input type="text" name="name"><br>
6
  • Why don't you edit your question and include what you've tried so far? Commented Nov 4, 2015 at 22:01
  • The wrote the entire bgcolor statement. All I can recall trying atm is the value="F". F for female Commented Nov 4, 2015 at 22:19
  • 1
    I'll be blunt: there are a lot of problems with this code. If we ignore all of them for a second and focus on the issue at hand, we still don't have enough information to help you. What's the name of the form field that contains the gender? What are the possible values? You need to edit your question with that information if you expect to get help. Commented Nov 4, 2015 at 22:28
  • 1
    Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example. Commented Nov 4, 2015 at 22:30
  • I gratefully accept your rebuke. I hope the the edited HTML section helps Commented Nov 4, 2015 at 22:34

1 Answer 1

3
<input type="radio" name="gen1" value="M"> male <br>
<input type="radio" name="gen2"  value="F"> female <br>

Radio groups need to share a name. You should also learn to love labels.

<label><input type="radio" name="sex" value="male"> male</label> <br>
<label><input type="radio" name="sex"  value="female"> female</label> <br>

Since you are using CGI, I've pulled in a CGI library. Note that CGI is not considered best practise and you should consider moving away from it.

The param method on a CGI object will let you read the form data from the query string.

Almost two decades ago, CSS was added to the web development stack. Use it for your presentation code.

Then set the class on the body based on your input data. This is best done using templates.

Note also: There is no "value" attribute for the body element.

#!/usr/bin/env perl

use strict;
use warnings;
use CGI; # I strongly suggest moving to Plack and FastCGI
use Template;

my $q = CGI->new;
my $sex = $q->param("sex");

if ((!defined $sex) || ($sex ne "male" && $sex ne "female")) {
    $sex = "";
}

print $q->header(-type => "text/html", -charset => 'utf-8');

my $template = Template->new;
$template->process(\*DATA, {sex => $sex});

__DATA__
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf=8">
        <title>Demo</title>
        <style>
            body {
                background-color: pink;
            }

            .male {
                background-color: green;
            }

            .female {
                background-color: yellow;
            }
       </style>
    </head>
    <body [% IF sex %]class="[% sex | html %]"[% END %]>
        <h1>Your content here</h1>
    </body>
</html>
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks!! this is what I was looking for!!
Hey sorry for the inconvenience but is there any way to have the name entered in the form field show up instead of " your content here" ? I am currently clueless. The script I had did this. It would return the name of the person after you clicked "submit" I appreciate you helping alot!
Use $q->param("NAME_OF_FIELD"), and then include it in the template in the same way as the other form data.
Print statement? String? No, that's nothing like what I did. You include it in the template data by editing the hasref on the line $template->process(\*DATA, {sex => $sex}); and you include it in the output by adding [% NAME_OF_THING | html %] to the template at the end.
@NavPal — If you're just getting started, then I suggest you take a look at Modern Perl, whatever resources you're learning from seem to be using the coding practises of the mid-1990s and aren't serving you well.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.