Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Perl code doesn't echo form data

by nrabus (Initiate)
on Mar 24, 2020 at 00:55 UTC ( [id://11114587]=perlquestion: print w/replies, xml ) Need Help??

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

I am in a Net-Centric computing course. We have a lab assignment to ask a user to enter data into a form and the Perl code should echo the data back. The HTML is not set up to validate data, so hitting submit without entering data will not trigger a validation message. My code causes the Perl code to be shown in the browser I tried running the html on 3 browsers. The data didn't echo with any of the 3 browsers.instead of echoing the form data. I entered my HTML and Perl programs below: The instructor gave me feedback and said to keep the "T" flag in the first line of my Perl code. Why does it show code instead of the form data? Thank you.

<!doctype html> <head> <meta charset="utf-8"> <title>index.html</title> <style type = "text/css"> body {background-color: lightblue;} div.background { background-image: url(images/10 pins.jpg); background-position: 100% center; background-origin: border-box; background-repeat: repeat; } .dropbtn { background-color: #4CAF50; color: white; padding: 16px; font-size: 16px; border: none; cursor: pointer; } /* The container <div> - needed to position the dropdown content */ .dropdown { position: relative; display: inline-block; } /* Dropdown Content (Hidden by Default) */ .dropdown-content { display: none; position: absolute; background-color: #f9f9f9; min-width: 160px; box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2); z-index: 1; } /* Links inside the dropdown */ .dropdown-content a { color: black; padding: 12px 16px; text-decoration: none; display: block; } /* Change color of dropdown links on hover */ .dropdown-content a:hover {background-color: #f1f1f1} /* Show the dropdown menu on hover */ .dropdown:hover .dropdown-content { display: block; } /* Change the background color of the dropdown button when the dropdow +n content is shown */ .dropdown:hover .dropbtn { background-color: #3e8e41; } </style> <meta name = "keywords" content = "bowling, club, central Illinois +"> <link href="styles/style.css" rel="stylesheet" type="text/css"> <!--The following script tag downloads a font from the Adobe Edge Web +Fonts server for use within the web page. We recommend that you do no +t modify it.--><script>var __adobewebfontsappname__="dreamweaver"</sc +ript><script src="http://use.edgefonts.net/adamina:n4:default.js" typ +e="text/javascript"></script> </head> <body> <div class ="background"> <header id="header"><h1>Central Illinois Bowling Club</h1></header +> <p><a href = "standings.html">Last Year's Standings</a></p><br +> <div style="background-color:lightgreen"><img src="images/ +bowling lane.png" width="293" height="187" alt="Bowling Lane"/> <img src="images/team.png" width="273" height="181" alt="T +eam"/> <img src="images/scoresheet.jpg" width="273" height="181" +alt="Scoresheet"/></div><br> <p> Welcome to the Central Illinois bowling Club site. We meet the thi +rd Saturday of the month from September through March. We decide on a + restaurant for dinner, the meet at a bowling alley. There are four b +owling centers we use and use the centers on a rotating basis. We wel +come persons of all ages who want to: <ul id="objectives"><li>A group who wants to build relationships</ +li> <li>A fellowship of those who like bowling or want to learn a +bout the sport</li> <li>Engage in a healthy hobby</li> </ul> Provide your name and phone number if you are interested. The coordina +tor (me) will contact you soon.<br><br> <FORM ACTION="cgi-bin/sample_register.cgi" METHOD="POST"> First name: <input type="text" name="fname"> Last name: <input type="text" name="lname"><br> <input type="radio" id="male" name="gender" value="male"> <label for="male">Male</label> <input type="radio" id="female" name="gender" value="female"> <label for="female">Female</label><br> <label for="age">Age (between 15 and 70):</label> <input type="number" id="age" name="age" min="15" max="70"><br> <label for="Skill">Select skill level:</label> <select id = "Skill"> <option value="Beginner">Beginner</option> <option value="Occasional">Occasional Bowler</option> <option value="Regular">Regular Bowler</option> <option value="HighAvg">High Average Bowler</option> </select><br> <p> Select other sports you participate in:<br> <input type="checkbox" name="sport" value="Baseball"> Baseball<br> <input type="checkbox" name="sport" value="Tennis"> Tennis<br> <input type="checkbox" name="sport" value="Golf"> Golf<br> <input type="checkbox" name="sport" value="Running"> Running<br> <input type="checkbox" name="sport" value="Football"> Football<br> +</p> <input type="submit" value="Submit"> <input type="reset" value="Reset"> </form> </div> </body></html>
#!/usr/bin/perl -wT use strict; use CGI; my $obj = new CGI; my $datastring =""; # read information from form my $fname = $obj->param( "fname" ); my $lname = $obj->param( "lname" ); my $gender = $obj->param( "gender" ); my $age = $obj->param( "age" ); my $Skill = $obj->param( "Skill" ); my $sport2 = ""; foreach my $sport (@sport) { $sport2 .= "$sport "; } # Save the data into a text file $datastring = "Saved Data\n\nFirst name: $fname\nLast name: $lname\nGe +nder: $gender\nAge: $age\nSkill: $Skill\nSports: $sport2\n\n"; open(OUTDATA, ">>", "data.txt") or die "Error in opening file data.txt +"; print OUTDATA $datastring; close(OUTDATA); #Send the info back print $obj->header( "text/html" ), $obj->start_html( -title => "Form Data", -topmargin =>"0" ), $obj->h1("User information"), $obj->p("User name: $fname"), $obj->p("Last name: $lname"); $obj->p("Gender: $gender"), $obj->p("Age: $age"), $obj->p("Skil: $skill"), $obj->p("Sports: $sport2"), $obj->end_html;

Replies are listed 'Best First'.
Re: Perl code doesn't echo form data
by kcott (Archbishop) on Mar 24, 2020 at 04:25 UTC

    G'day nrabus,

    Welcome to the Monastery.

    From your description, it sounds like the problem lies with you web server configuration. I don't know what you're using but I'll take a guess at Apache. You need to specify in your configuration file — possibly named httpd.conf — that the cgi-bin directory has Perl code which the web server should execute. I can't tell you exactly what the configuration code would be; however, it might look something like:

    <Directory "/cgi-bin"> SetHandler cgi-script Options +ExecCGI </Directory>

    There's quite possibly other directives you'll need. I'm not really comfortable guessing any further.

    If it is Apache, the default httpd.conf provides some clues. Get details from "Apache HTTP Server Version 2.4 Documentation"; although, you may need a different version. See the "Directive Index" for links to the documentation for individual directives.

    I only briefly looked at your code. You generally seem to be on the right track. You've got a semicolon where you probably should have a comma after the "Last name:" part; alternatively, add another print after it (i.e. just before the "Gender:" part).

    You also might like to consult the "CGI" and "CGI::HTML::Functions" documentation.

    — Ken

      This.

      It's not a Perl problem or an HTML problem. The web server is misconfigured, or at least there is a mismatch between how it's been configured and how you're trying to use it.

      And that's assuming you're accessing the page through a web server at all. If your web browser is accessing the pages via file:// URLs, this will not work. You need to be accessing the pages over HTTP or HTTPS.

        I'm using a brpwser. I will fix the URL.
      Ken, Thank you. I have Apache but didn't have the server started. I will try it. BTW, I have Win 10 with Apache downloaded from official site. Nick'

        I strongly recommend that you get into the habit of checking the Apache access and error logs. You should do this after every test and following all code and config changes: it's often the case that what looks OK from a visual inspection of the web page actually has one or more (not-so-obvious) underlying issues; for instance, perhaps you're accessing code, data, or other resources, from the wrong path.

        And, of course, if the last message in the access log is about the server shutting down, that's a huge clue to the need to actually start the server. :-)

        I provided some Apache-related help a couple of months ago: "Re: configuring mod_perl on Apache2.4 (conf & virtual host help)?". That was mainly related to virtual hosts and nothing to do with your issue; however, there may be a few things you'll find useful — for example, setting up *.bat scripts with similar functionality to the aliases I described may make your life easier.

        — Ken

Re: Perl code doesn't echo form data
by soonix (Canon) on Mar 24, 2020 at 09:21 UTC
Re: Perl code doesn't echo form data
by 1nickt (Canon) on Mar 24, 2020 at 01:57 UTC

    Hi, it's great that you are getting some exposure to Perl in your class, but your instructor has you using a 20-yr old technique and toolset to write a 21st Century page. Too bad; you could have much more fun building your app with something like Dancer2. You should show it to him/her ;-)

    I haven't worked with CGI for a long, long time, but don't you need to print your HTML elements after generating them, as you do with your header?

    Hope this helps!


    The way forward always starts with a minimal test.
      Thank you

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11114587]
Approved by Athanasius
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (2)
As of 2024-04-26 03:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found