944,081 Members | Top Members by Rank

Ad:
  • Perl Discussion Thread
  • Marked Solved
  • Views: 3289
  • Perl RSS
Oct 2nd, 2007
0

getting form elements to write to a file

Expand Post »
whats up everybody? havent been on daniweb in a while. self-motivation like whut....until now. anyways ive got this script that makes an html form with 19 or so fields. its supposed to put the form info into a file (mediaRequest_log.txt). it gets this information by way of

perl Syntax (Toggle Plain Text)
  1. # get form info from POST method
  2. read(STDIN, $text, $ENV{'CONTENT_LENGTH'});
  3.  
  4. my @value_pairs = split (/&/,$text);
  5. my %form_results = ();
  6.  
  7. foreach $pair (@value_pairs) {
  8. ($key, $value) = split (/=/,$pair);
  9. $value =~ tr/+/ /;
  10. $value =~ s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C", hex ($1))/eg;
  11. $form_results{$key} = $value; # store the key in the results hash
  12. }

and then to put it into a variable and append it to a file

perl Syntax (Toggle Plain Text)
  1. foreach $key (sort keys(%form_results)) {
  2. # print "$key has value $form_results{$key}<BR>\n";
  3. # $mail_string .= "$key = $form_results{$key}\n";
  4. $file_string .= "$form_results{$key}|";
  5. }
  6.  
  7. chop($file_string);
  8.  
  9. # add newline to end of $file_string
  10. $file_string .= "\n";
  11.  
  12. $filename = "mediaRequest_log.txt";
  13. open(DAT,">>$filename") || die("Cannot Open File");
  14. print DAT "$file_string";
  15. close(DAT);

now i also use this

perl Syntax (Toggle Plain Text)
  1. use CGI;
  2. $query = new CGI;

to format the form element values for a email string ($mail_string) and a display string to display the results when the submit button is clicked ($display_string). long story short, this script works fine except that it does not write anything to the file except for a blank line. why is this happeneing???? can i not use both methods together? if thats not so, how can i fix my script to make it write the form element values to the text file? your help is greatly appreciated.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
sickly_man is offline Offline
42 posts
since Jun 2007
Oct 2nd, 2007
0

Re: getting form elements to write to a file

perl Syntax (Toggle Plain Text)
  1. #!/usr/local/bin/perl
  2.  
  3. # Simple Email Function
  4. # ($to, $from, $subject, $message)
  5. sub sendEmail
  6. {
  7. my ($to, $from, $subject, $message) = @_;
  8. my $sendmail = '/usr/lib/sendmail';
  9. open(MAIL, "|$sendmail -oi -t");
  10. print MAIL "From: $from\n";
  11. print MAIL "To: $to\n";
  12. print MAIL "Subject: $subject\n\n";
  13. print MAIL "$message\n";
  14. close(MAIL);
  15. }
  16.  
  17. use CGI;
  18. $query = new CGI;
  19.  
  20. # get username
  21. $user = $ENV{'REMOTE_USER'};
  22.  
  23. if ( $ENV{'CONTENT_LENGTH'} > 0 )
  24. {
  25. # ~~~~~~~~~~~~~~~~~~~~~~ GETTING FORM INFO FOR $file_string ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  26.  
  27. # get form info from POST method
  28. read(STDIN, $text, $ENV{'CONTENT_LENGTH'});
  29.  
  30. my @value_pairs = split (/&/,$text);
  31. my %form_results = ();
  32.  
  33. foreach $pair (@value_pairs) {
  34. ($key, $value) = split (/=/,$pair);
  35. $value =~ tr/+/ /;
  36. $value =~ s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C", hex ($1))/eg;
  37. $form_results{$key} = $value; # store the key in the results hash
  38. }
  39.  
  40. print "Content-type: text/html\n\n";
  41. print "<html><body>\n";
  42.  
  43. # loop through the results and print each key/value
  44. # also make $file_string from each key/value
  45. foreach $key (sort keys(%form_results)) {
  46. # print "$key has value $form_results{$key}<BR>\n";
  47. # $mail_string .= "$key = $form_results{$key}\n";
  48. $file_string .= "$form_results{$key}|";
  49. }
  50.  
  51. chop($file_string);
  52.  
  53. # add newline to end of $file_string
  54. $file_string .= "\n";
  55.  
  56. $filename = "mediaRequest_log.txt";
  57. open(DAT,">>$filename") || die("Cannot Open File");
  58. print DAT "$file_string";
  59. close(DAT);
  60.  
  61. print "Here is the file string: " . $file_string;
  62.  
  63. # ~~~~~~~~~~~~~~~~~~~~~ ADD INFO TO $mail_string ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  64.  
  65. $mail_string = "User: " . $query->param('user') . "\n";
  66. $mail_string .= "Date of Media Request: " . $query->param('date') . "\n";
  67. $mail_string .= "Requested by: " . $query->param('req_by') . "\n";
  68. $mail_string .= "Ship-to Address: " . $query->param('ship_to') . "\n";
  69. if ( $query->param('is_free') eq "on" )
  70. {
  71. $mail_string .= "Is this a free EO3 webcast? YES \n";
  72. }
  73. else
  74. {
  75. $mail_string .= "Is this a free EO3 webcast? NO \n";
  76. $mail_string .= "Billing Address: " . $query->param('bill_to') . "\n";
  77. }
  78. $mail_string .= "Date of Program Requested: " . $query->param('dateOfProg') . "\n";
  79. $mail_string .= "Title/Description of Program: " . $query->param('title') . "\n\n";
  80. $mail_string .= "PHYSICAL MEDIA PREFERENCES\n";
  81. $mail_string .= "Number of CDs Requested: " . $query->param('cd') . "\n";
  82. $mail_string .= "Number of DVDs Requested: " . $query->param('dvd') . "\n";
  83. $mail_string .= "Number of VHS Tapes Requested: " . $query->param('vhs') . "\n\n";
  84. $mail_string .= "FILE FORMATS REQUESTED\n";
  85. if ( $query->param('windows') eq "on" )
  86. {
  87. $mail_string .= "Include Windows files.\n";
  88. }
  89. if ( $query->param('real') eq "on" )
  90. {
  91. $mail_string .= "Include Real files.\n";
  92. }
  93. if ( $query->param('quicktime') eq "on" )
  94. {
  95. $mail_string .= "Include Quicktime files.\n";
  96. }
  97. if ( $query->param('caption') eq "on" )
  98. {
  99. $mail_string .= "Include Caption files.\n";
  100. }
  101. $mail_string .= "\nUrgency: " . $query->param('urgency') . "\n";
  102. $mail_string .= "Selected Shipping Method: " . $query->param('shipping') . "\n";
  103. $mail_string .= "Special Instructions: " . $query->param('specInstr') . "\n";
  104. $mail_string .= "E-mailed to: " . $query->param('email_to') . "\n";
  105.  
  106. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ END SECTION ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  107.  
  108. # ~~~~~~~~~~~~~~~~~~~ ADD INFO TO $display_string ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  109.  
  110. $display_string = "User: " . $query->param('user') . "<br>";
  111. $display_string .= "Date of Media Request: " . $query->param('date') . "<br>";
  112. $display_string .= "Requested by: " . $query->param('req_by') . "<br>";
  113. $display_string .= "Ship-to Address: " . $query->param('ship_to') . "<br>";
  114. if ( $query->param('is_free') eq "on" )
  115. {
  116. $display_string .= "Is this a free EO3 webcast? YES <br>";
  117. }
  118. else
  119. {
  120. $display_string .= "Is this a free EO3 webcast? NO <br>";
  121. $display_string .= "Billing Address: " . $query->param('bill_to') . "<br>";
  122. }
  123. $display_string .= "Date of Program Requested: " . $query->param('dateOfProg') . "<br>";
  124. $display_string .= "Title/Description of Program: " . $query->param('title') . "<p>";
  125. $display_string .= "<b>PHYSICAL MEDIA PREFERENCES</b><br>";
  126. $display_string .= "Number of CDs Requested: " . $query->param('cd') . "<br>";
  127. $display_string .= "Number of DVDs Requested: " . $query->param('dvd') . "<br>";
  128. $display_string .= "Number of VHS Tapes Requested: " . $query->param('vhs') . "<p>";
  129. $display_string .= "<b>FILE FORMATS REQUESTED</b><br>";
  130. if ( $query->param('windows') eq "on" )
  131. {
  132. $display_string .= "Include Windows files.<br>";
  133. }
  134. if ( $query->param('real') eq "on" )
  135. {
  136. $display_string .= "Include Real files.<br>";
  137. }
  138. if ( $query->param('quicktime') eq "on" )
  139. {
  140. $display_string .= "Include Quicktime files.<br>";
  141. }
  142. if ( $query->param('caption') eq "on" )
  143. {
  144. $display_string .= "Include Caption files.<br>";
  145. }
  146. $display_string .= "<p>Urgency: " . $query->param('urgency') . "<br>";
  147. $display_string .= "Selected Shipping Method: " . $query->param('shipping') . "<br>";
  148. $display_string .= "Special Instructions: " . $query->param('specInstr') . "<br>";
  149. $display_string .= "E-mailed to: " . $query->param('email_to') . "<p>";
  150.  
  151. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ END SECTION ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  152.  
  153. sendEmail( $query->param('email_to'), "sara\@aanet.org", "New Media Request.",
  154. $mail_string );
  155.  
  156. print "<h3>Here is the new Media Request you just added.</h3><p>";
  157.  
  158. print "<img src=\"http://gothics.aanet.org/images/eagle1.gif\" align=\"right\" alt=\"Great Job!\"><p>";
  159. print "<h3 align=\"right\">Great Job!</h3>";
  160.  
  161. print $display_string;
  162.  
  163. # ~~~~~~~~~~~~~~~~~~ OLD display media request you just added ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  164. # print "<table border=\"2\">";
  165.  
  166. # foreach $key (sort keys(%form_results)) {
  167. # print "<th>$key</th>";
  168. # }
  169.  
  170. # print "<tr>";
  171. # foreach $key (sort keys(%form_results)) {
  172. # print "<td>$form_results{$key}</td>";
  173. # }
  174. # print "</tr>";
  175.  
  176. # print "</table><p>";
  177. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  178.  
  179.  
  180. # link back to main menu
  181. print "<a href=\"http://gothics.aanet.org/cgi-bin/media/mediamenu.cgi\">Main Menu</a>";
  182.  
  183. print "</body></html>\n";
  184. }
  185. else
  186. {
  187. # get time info from perl's time function
  188. @timeData = localtime(time);
  189. $timeData[4] += 1;
  190. $timeData[5] += 1900;
  191.  
  192. print "Content-type: text/html\n\n";
  193. print "<html>\n";
  194. print "<body>\n";
  195.  
  196. print "<h2>Make a New Media Request.</h2>\n";
  197.  
  198. print "<form name=\"media_request\" method=\"post\" action=\"request.cgi\">";
  199.  
  200. print "<img src=\"http://gothics.aanet.org/images/eddie_van_halen_jump.jpg\" align=\"right\" alt=\"Jump\">";
  201.  
  202. print "User: <input type=\"text\" name=\"user\" value=\"$user\"><p>\n";
  203. print "Date: <input type=\"text\" name=\"date\" value=\"$timeData[4]/$timeData[3]/$timeData[5]\"><p>\n";
  204. print "Requested by: <input type=\"text\" name=\"req_by\"><p>\n";
  205. print "Ship-to Address: <input type=\"text\" size=\"60\" name=\"ship_to\"><p>\n";
  206.  
  207. print "Complimentary copy of EO3 webcast? (one per member): <input type=\"checkbox\" name=\"is_free\">
  208. Yes (if not, leave unchecked)<p>\n";
  209.  
  210. print "If not free, send bill to (same as ship-to): <input type=\"text\" name=\"bill_to\" size=\"60\"><p>\n";
  211. print "Date of program requested: <input type=\"text\" name=\"dateOfProg\" size=\"35\"><p>\n";
  212. print "Description/Title of program: <input type=\"text\" size=\"60\" name=\"title\"><p>\n";
  213.  
  214. print "<b>Choose your media preferences below.</b><p>";
  215. print "How many CDs?: <input type=\"text\" name=\"cd\"><p>\n";
  216. print "How many DVDs?: <input type=\"text\" name=\"dvd\"><p>\n";
  217. print "How many VHS tapes?: <input type=\"text\" name=\"vhs\"><p>\n";
  218.  
  219. print "<b>Standard files to be included: </b><p>
  220. <input type=\"checkbox\" checked=\"yes\" name=\"windows\"> Windows<p>\n" .
  221. "<input type=\"checkbox\" checked=\"yes\" name=\"real\"> Real<p>\n" .
  222. "<input type=\"checkbox\" checked=\"yes\" name=\"quicktime\"> Quicktime<p>\n" .
  223. "<input type=\"checkbox\" checked=\"yes\" name=\"caption\"> Captioning<p>\n";
  224.  
  225. print "Urgency: <input type=\"text\" name=\"urgency\"><p>\n";
  226.  
  227. print "Please choose a shipping method: <select name=\"shipping\">" .
  228. "<option value=\"\">Shipping Methods</option>" .
  229. "<option value=\"DHL-next day by 10:30am\">DHL next day by 10:30am</option>" .
  230. "<option value=\"DHL-next day by noon\">DHL next day by noon</option>" .
  231. "<option value=\"DHL-next day by 3:00pm \">DHL next day by 3:00pm</option>" .
  232. "<option value=\"DHL-2 day\">DHL 2 day</option>" .
  233. "<option value=\"USPS\">USPS (not recommended)</option>";
  234. print "</select><p>\n";
  235.  
  236. print "Special Instructions: <input type=\"text\" name=\"specInstr\" size=\"60\"><p>\n";
  237. print "E-mail to: <input type=\"text\" name=\"email_to\"><p>\n";
  238.  
  239. print "<input type=\"submit\" name=\"submit_all\" value=\"Submit\"><p>\n";
  240.  
  241. print "</form>\n";
  242. print "</body></html>\n";
  243. }
Reputation Points: 10
Solved Threads: 0
Light Poster
sickly_man is offline Offline
42 posts
since Jun 2007
Oct 2nd, 2007
0

Re: getting form elements to write to a file

why are you mixing that terrible form parsing code in with CGI anyway? Use the CGI module to get all the form data, don't mix it with that insecure code you are using. Also, when the script reads the data from STDIN:

read(STDIN, $text, $ENV{'CONTENT_LENGTH'});

it empties the buffer, so any subsequent attemptsto read the same data will fail. That may explain your situation, but i did not take a close look at your code.
Reputation Points: 246
Solved Threads: 67
Practically a Posting Shark
KevinADC is offline Offline
898 posts
since Mar 2006
Oct 2nd, 2007
0

Re: getting form elements to write to a file

i am fairly new to perl. can you explain what you mean by this?

"why are you mixing that terrible form parsing code in with CGI anyway? Use the CGI module to get all the form data, don't mix it with that insecure code you are using."
Reputation Points: 10
Solved Threads: 0
Light Poster
sickly_man is offline Offline
42 posts
since Jun 2007
Oct 2nd, 2007
0

Re: getting form elements to write to a file

This is the terrible code I refer to:

Perl Syntax (Toggle Plain Text)
  1. if ( $ENV{'CONTENT_LENGTH'} > 0 )
  2. {
  3. # ~~~~~~~~~~~~~~~~~~~~~~ GETTING FORM INFO FOR $file_string ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. # get form info from POST method
  5. read(STDIN, $text, $ENV{'CONTENT_LENGTH'});
  6. my @value_pairs = split (/&/,$text);
  7. my %form_results = ();
  8. foreach $pair (@value_pairs) {
  9. ($key, $value) = split (/=/,$pair);
  10. $value =~ tr/+/ /;
  11. $value =~ s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C", hex ($1))/eg;
  12. $form_results{$key} = $value; # store the key in the results hash
  13. }

You have loaded the CGI module and created a CGI object ($query)


Perl Syntax (Toggle Plain Text)
  1. use CGI;
  2.  
  3. $query = new CGI;

That is what you shoud use to get all the form data, like you do in some parts of the code:

Perl Syntax (Toggle Plain Text)
  1. $query->param('bill_to')
Reputation Points: 246
Solved Threads: 67
Practically a Posting Shark
KevinADC is offline Offline
898 posts
since Mar 2006
Oct 2nd, 2007
0

Re: getting form elements to write to a file

Click to Expand / Collapse  Quote originally posted by KevinADC ...
This is the terrible code I refer to:

Perl Syntax (Toggle Plain Text)
  1. if ( $ENV{'CONTENT_LENGTH'} > 0 )
  2. {
  3. # ~~~~~~~~~~~~~~~~~~~~~~ GETTING FORM INFO FOR $file_string ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. # get form info from POST method
  5. read(STDIN, $text, $ENV{'CONTENT_LENGTH'});
  6. my @value_pairs = split (/&/,$text);
  7. my %form_results = ();
  8. foreach $pair (@value_pairs) {
  9. ($key, $value) = split (/=/,$pair);
  10. $value =~ tr/+/ /;
  11. $value =~ s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C", hex ($1))/eg;
  12. $form_results{$key} = $value; # store the key in the results hash
  13. }

You have loaded the CGI module and created a CGI object ($query)


Perl Syntax (Toggle Plain Text)
  1. use CGI;
  2.  
  3. $query = new CGI;

That is what you shoud use to get all the form data, like you do in some parts of the code:

Perl Syntax (Toggle Plain Text)
  1. $query->param('bill_to')

well i was using that because it was a fast and easy way to get all the form elements into a string delimited by a '|' character. u keep saying its terrible code.....but why is it terrible code? believe me i would like to do everything using $query->param('watever') but id rather not write out each element to just put it in a string.

btw i was able to do what i want to do without using

perl Syntax (Toggle Plain Text)
  1. use cgi;
  2. $query = new CGI;

if you know of an easy way to get all the form element values into a string delimited by '|' then please share it!!!!
Reputation Points: 10
Solved Threads: 0
Light Poster
sickly_man is offline Offline
42 posts
since Jun 2007
Oct 2nd, 2007
0

Re: getting form elements to write to a file

untested but should work

Perl Syntax (Toggle Plain Text)
  1. use CGI;
  2. my $query = CGI->new;
  3. my %form_results = $query->Vars;
  4. my $file_string = join ('|',map {$form_results{$_}} sort keys %form_results);
  5. print $file_string;

read the CI module documentation. I know it is long and some parts are a bit hard to follow, but the part about importing all the params as a list or hash or array is easy to understand.
Reputation Points: 246
Solved Threads: 67
Practically a Posting Shark
KevinADC is offline Offline
898 posts
since Mar 2006
Oct 3rd, 2007
0

Re: getting form elements to write to a file

thank you for your help that bit of code works great. i still would like to know what is so bad about the other code i was using.....you said it was insecure. what makes it insecure? i guess my question is why should i use the CGI module instead, besides the fact taht its easier.
Reputation Points: 10
Solved Threads: 0
Light Poster
sickly_man is offline Offline
42 posts
since Jun 2007
Oct 3rd, 2007
0

Re: getting form elements to write to a file

Some reading if you are interested:

http://users.easystreet.com/ovid/cgi...esson_two.html

You should also be using Taint mode with all CGI scripts.

Ask on www.perlmonks.com why you should not use the form parsing code you posted above and you should get some more opinions. Mine is my own, the opinion of others may vary. Exposure to more opinions may at first be confusing but hopefully it will be educational.
Reputation Points: 246
Solved Threads: 67
Practically a Posting Shark
KevinADC is offline Offline
898 posts
since Mar 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Perl Forum Timeline: HELP: AoH to HoH pulling its no value
Next Thread in Perl Forum Timeline: can anyone help me to debug this script





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC