Advertisement
Guest User

part2.cgi

a guest
Feb 19th, 2013
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2.  
  3. #print a standard 200-levlel HTTP Header
  4. print "Content-Type:text/html\n\n";
  5.  
  6. if ($ENV{'REQUEST_METHOD'} eq "GET") {
  7.  
  8. #display form if GET request is used
  9. &survey();
  10. #}elsif ($ENV{'REQUEST_METHOD'} eq "POST") {
  11. #Process form if POST method used
  12. # &displayInfo();
  13. }
  14.  
  15.  
  16.  
  17. #the following code - in blue - wiil
  18. #get the data from the form, and store it in a hash named %form
  19.  
  20. #get data from env var
  21. $qstring = $ENV{'QUERY_STRING'};
  22.  
  23. #break data up on ampersands, and store it in an array
  24. @pairs = split(/&/, $qstring);
  25.  
  26. #start a loop to process form data
  27. foreach (@pairs) {
  28. # split field name and value on '=', store in two scalar vars
  29. ($key, $value) = split(/=/);
  30. #translate '+' signns back to spaces
  31. $value =~ tr/+/ /;
  32. #translate special characters
  33. $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  34. #store data in hash
  35. $form{$key} = $value;
  36. }
  37.  
  38. #now the data is stored in the hash $form
  39.  
  40. #send output to browser as HTML
  41.  
  42. print "<html><head><title>Student Survey</title></head>\n";
  43. print "<body>\n";
  44.  
  45. sub survey {
  46. print qq~
  47. <h1>Student Survey</h1>
  48. <form action="/cgi-stuff/part2.cgi" method="GET">
  49. Full Name: <input type="text" name="person"><br>
  50. Favourite Sport: <input type="text" name="sport"><br>
  51. Favourite Seneca Course: <input type="text" name="course"><br>
  52. Current GPA: <input type="text" name="gpa"><br>
  53. <input type="submit" value="send">
  54. <input type="reset" value="reset">
  55. </form>
  56.  
  57. ~;
  58. }
  59.  
  60.  
  61. #display survey
  62. #&survey();
  63.  
  64.  
  65. #display form data
  66. &displayInfo();
  67.  
  68. print "</body></html>\n";
  69.  
  70. #This subroutine will display info received from a form
  71.  
  72. sub displayInfo {
  73. print "Full Name:", $form{"person"}, "<br>";
  74. print "Favourite Sport:", $form{"sport"}, "<br>";
  75. print "Favourite Seneca Course:", $form{"course"}, "<br>";
  76. print "GPA:", $form{"course"}, "<br>";
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement