User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Perl section within the Software Development category of DaniWeb, a massive community of 456,520 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,814 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Perl advertiser: Programming Forums
Views: 1707 | Replies: 8 | Solved
Reply
Join Date: Jun 2007
Location: Albany, NY
Posts: 42
Reputation: sickly_man is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
sickly_man's Avatar
sickly_man sickly_man is offline Offline
Light Poster

getting form elements to write to a file

  #1  
Oct 2nd, 2007
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

  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. }
  13.  

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

  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);
  16.  

now i also use this

  1. use CGI;
  2. $query = new CGI;
  3.  

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.
Henry Phillips
Web Programmer
Adirondack Area Network, LLC
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2007
Location: Albany, NY
Posts: 42
Reputation: sickly_man is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
sickly_man's Avatar
sickly_man sickly_man is offline Offline
Light Poster

Re: getting form elements to write to a file

  #2  
Oct 2nd, 2007
  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. }
  244.  
Henry Phillips
Web Programmer
Adirondack Area Network, LLC
Reply With Quote  
Join Date: Mar 2006
Posts: 642
Reputation: KevinADC is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 36
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Master Poster

Re: getting form elements to write to a file

  #3  
Oct 2nd, 2007
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.
Reply With Quote  
Join Date: Jun 2007
Location: Albany, NY
Posts: 42
Reputation: sickly_man is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
sickly_man's Avatar
sickly_man sickly_man is offline Offline
Light Poster

Re: getting form elements to write to a file

  #4  
Oct 2nd, 2007
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."
Henry Phillips
Web Programmer
Adirondack Area Network, LLC
Reply With Quote  
Join Date: Mar 2006
Posts: 642
Reputation: KevinADC is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 36
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Master Poster

Re: getting form elements to write to a file

  #5  
Oct 2nd, 2007
This is the terrible code I refer to:

if ( $ENV{'CONTENT_LENGTH'} > 0 )
{
# ~~~~~~~~~~~~~~~~~~~~~~ GETTING FORM INFO FOR $file_string ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# get form info from POST method
read(STDIN, $text, $ENV{'CONTENT_LENGTH'});
my @value_pairs = split (/&/,$text);
my %form_results = ();
foreach $pair (@value_pairs) {
($key, $value) = split (/=/,$pair);
$value =~ tr/+/ /;
$value =~ s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C", hex ($1))/eg;
$form_results{$key} = $value; # store the key in the results hash
}

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


use CGI;

$query = new CGI;

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

$query->param('bill_to')
Reply With Quote  
Join Date: Jun 2007
Location: Albany, NY
Posts: 42
Reputation: sickly_man is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
sickly_man's Avatar
sickly_man sickly_man is offline Offline
Light Poster

Re: getting form elements to write to a file

  #6  
Oct 2nd, 2007
Originally Posted by KevinADC View Post
This is the terrible code I refer to:

if ( $ENV{'CONTENT_LENGTH'} > 0 )
{
# ~~~~~~~~~~~~~~~~~~~~~~ GETTING FORM INFO FOR $file_string ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# get form info from POST method
read(STDIN, $text, $ENV{'CONTENT_LENGTH'});
my @value_pairs = split (/&/,$text);
my %form_results = ();
foreach $pair (@value_pairs) {
($key, $value) = split (/=/,$pair);
$value =~ tr/+/ /;
$value =~ s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C", hex ($1))/eg;
$form_results{$key} = $value; # store the key in the results hash
}

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


use CGI;

$query = new CGI;

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

$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

  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!!!!
Henry Phillips
Web Programmer
Adirondack Area Network, LLC
Reply With Quote  
Join Date: Mar 2006
Posts: 642
Reputation: KevinADC is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 36
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Master Poster

Re: getting form elements to write to a file

  #7  
Oct 2nd, 2007
untested but should work

use CGI;
my $query = CGI->new;
my %form_results = $query->Vars;
my $file_string = join ('|',map {$form_results{$_}} sort keys %form_results);
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.
Reply With Quote  
Join Date: Jun 2007
Location: Albany, NY
Posts: 42
Reputation: sickly_man is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
sickly_man's Avatar
sickly_man sickly_man is offline Offline
Light Poster

Re: getting form elements to write to a file

  #8  
Oct 3rd, 2007
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.
Henry Phillips
Web Programmer
Adirondack Area Network, LLC
Reply With Quote  
Join Date: Mar 2006
Posts: 642
Reputation: KevinADC is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 36
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Master Poster

Re: getting form elements to write to a file

  #9  
Oct 3rd, 2007
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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Perl Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Perl Forum

All times are GMT -4. The time now is 4:07 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC