Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

saving data problem

by cal (Beadle)
on Oct 19, 2002 at 08:01 UTC ( #206491=perlquestion: print w/replies, xml ) Need Help??

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

Hello Monks, I have a simple html form.(Month Day Year Title and Message) input fields are used.
<form method="POST" action="http://www.dom.com/cgi-local/events.cgi"> <input type="hidden" name="action" value="preview"> <input type="submit" value="Preview" name="B1"> <input type="reset" value="Reset" name="B2"> </form>
When the form is submited the cgi script displays the variable information and formatting correctly.
#!/usr/local/bin/perl -w use strict; use diagnostics; use CGI qw(:standard); my $q = new CGI; my $action = $q->param('action'); ############################# #initialize form variables ############################# #my $uniq_number = &uniqueid; my $month = param('month'); my $date = param('date'); my $year = param('year'); my $title = param('title'); my $message = param('message'); my $placement = param('placement'); #PLACES NEW EVENT BEFORE OR AFTER EXISTING EVENTS ################################ # variables for storage ############################### my $data = join '::', $month,$date,$year,$title,$message; my $addfile = "events.txt"; my $line = ""; ###################################### #BEGIN COUNTCURRENTRECORDS SUBROUTINE ###################################### open(FILE, "$addfile"); my @lines = <FILE>; close(FILE); my $num = @lines; ###################################### if($action =~ /add_event/i){ &store_event; } ############################## # Print New Event for approval ############################## # Add the event passed variables from the add event page print header(), start_html(-title=>'CGI Example Script'), h2('New Parish Event!'), "<HR> <table border='0' width='100%'> <tr> <td width='100%'> <table border='1' width='100%'> <tr> <td width='100%'> <table border='1' width='100%'> <tr> <td width='22%'> <font SIZE='+1'color='gray'><B>$month $date, $year</B> </font></td> <td width='78%'> <font SIZE='+1'color='black'> <B>$title</B></font></td> </tr> </table> </td> </tr> <tr> <td width='800%'><font SIZE='2'>$message</font> </td> </tr> </table> </td> </tr> </table> <BR> <form action method='post'> <input type='hidden' name='action' value='add_event'> <input type='submit' value=' Publish new event '> <BR> <INPUT type='button' value='Continue'onClick='history.go(-1)'> </form>", end_html(); ##################################### #could be sub for saving data #################################### ##################################### # Open the file for appending ################################### sub store_event{ if ($placement eq "beg") { open (FH, "<events.txt") || die "could not open file: $!"; my @ODB = <FH>; close (FH); my $newline = "$data"; open (NFH, ">events.txt") || die "could not open file 2: $!"; print NFH "$newline" . "\n"; foreach $line (@ODB) { print NFH "$line"; } close (NFH); } else { open (OUT, ">>$addfile") or die "Can't open $addfile\n"; print OUT "$data\n"; close OUT; } } exit (0);
At this point I wanted to be able to write the variables to a file or go back and continue editing. But I am still new to perl and I am having a lot of trouble. Thanks for any help Cal

Replies are listed 'Best First'.
Re: saving data problem
by atcroft (Abbot) on Oct 19, 2002 at 09:11 UTC

    I'm not sure I follow the problem. You said (code removed):

    Hello Monks, I have a simple html form.(Month Day Year Title and Message) input fields are used. When the form is submited the cgi script displays the variable information and formatting correctly. At this point I wanted to be able to write the variables to a file or go back and continue editing. But I am still new to perl and I am having a lot of trouble.

    I have not tried to run the code you posted, so I am not sure if what you ask is a problem in the code itself, or when it runs. Assuming the code runs ok,

    • Is the problem you are having with the actual saving of the data to the file? If so, what error are you seeing? One common issue with writing to data files from a CGI is the ownership and read/write permissions on the data files and the directory they reside in.
    • Is the issue with data being clobbered during submission? You may want to look at implementing some form of locking to prevent two submissions from walking over each other.
    • Is the problem with having the values back in the form to be edited? My own limited experience has been to make scripts that keyed off a value returned by the submit to determine which processing to do, in order to also display the form with the elements' VALUE= lines filled in (for instance, having your Preview and Submit buttons have the same NAME= value, such as "dowhat", setting $action to some default value such as "donothing", then changing it to "add_event" only if defined($q->param('dowhat')) and it has the appropriate value-otherwise, if I read your code right, it should go ahead and display the form anew). If I read it correctly, it looks like you're using a bit of javascript to send them back a page on a "Continue", which in my limited understanding I would think would cause them to lose what they had typed.
    • Is the issue something else? As I said, I wasn't sure from what you asked.

    While I was a little concerned at seeing values in store_event() that had not been passed it, I realized the concern was more likely due to the time of night I was reading the posting and the likelyhood you had stripped the code down somewhat than just they not actually being defined in the code.

    A look at Ovid's Web Programming Using Perl course, and tachyon's CGI Help Guide might give you some additional insight into the issue. CGI Programming with Perl and the Perl Cookbook may also prove helpful, as may other resources, such as the Tutorials section here and the comments of other monks.

    Good luck with the problem. Hope this helped somewhat.

    Update: (19 Oct 2002) In several private messages from others overnight, it was suggested to me several other sources which might also be of use, or that might be of use to others new to the site who may read this later. Mostly related to the asking of questions, they included:

    To you, and to others who come after, I wish you good luck, and my thanks to those who messaged me regarding the additional information.

Re: saving data problem
by Bilbo (Pilgrim) on Oct 19, 2002 at 08:42 UTC
    Could you be a bit more specific about what problem you are having? Do you have any error messages? Is writing to the file causing the problems? If so then I would suspect a permissions problem. CGI scripts tend not to run with your user id but as processes belonging to another user (often 'nobody'), so you have to make sure that this user has permission to write to your file.
Re: saving data problem
by George_Sherston (Vicar) on Oct 19, 2002 at 14:21 UTC
    Like other monks I don't get exactly what you want to do. But if *all* you want to do is dump the data from your form into a file then you could do worse than use Data::Dumper to put the whole CGI object, containing everything you got from the form, into a text file. This would do it:
    use Data::Dumper; my $filename = time; # i.e. a time-stamped filename $filename .= '.txt'; open F, ">/your/chose/path/$filename"; print F Dumper($q);


    George Sherston
Re: saving data problem
by cal (Beadle) on Oct 19, 2002 at 17:45 UTC
    I guess the problem I am having is that when I look at the data file it only has the seperators in the added line (::::::::)the actual data is missing. Also the data is no longer displayed of formated correctly on the screen. Cal

      Looking at the code again, I noticed you had (comments removed):

      my $q = new CGI; my $action = $q->param('action'); my $month = param('month'); my $date = param('date'); my $year = param('year'); my $title = param('title'); my $message = param('message'); my $placement = param('placement');

      I believe the issue is that you're retrieving them as my $variable = param('key'); when you should have it (I believe) as my $variable = $q->param('key'); for those lines. Try that, and see if it might take care of the issue for you. Just a thought.

      Update (19 Oct 2002): In attempting to run the script from the command-line to figure out what else might be wrong, I received an error message about $placement not being defined, using the code you posted above. Did you leave out the code that set the value for that? Testing from the command line (-debug option required for this in the use statement for some versions of CGI.pm), when action was passed as 'add_event', what was written to the file was what I handed it for all of the other values. Are you seeing any error messages in your logs?

      Update (19 Oct 2002): Something else I'm goig to ask just to ask, but I didn't see the elements you were passing in the form in the posting. Did you remove the <input> tags from the sample form? Those tags would have to appear between the <form></form> elements... Just thought I would ask...

        Thanks, No change. Still(::::::::)
        I am really sorry. I should have just included the html page. Better late than never I hope.
        <html> <head> <title>Add Event</title> </head> <body> <p><b><u><font color="#CC9900">ADD UPCOMING EVENTS</font></u></b></p> <hr> <table border="1" width="100%"> </table> <form method="POST" action="http://www.dom.com/cgi-local/homepage_upda +te/events/upcoming_events.cgi"> <table border="1" width="100%" height="93" bgcolor="#C0C0C0"> <tr> <td width="100%" height="87"> <table border="0" width="100%"> <tr> <td width="13%"><b><font size="5"> ADD EVENT SCREEN </font></b></td> <td width="87%"> <table border="1" width="100%" height="99"> <tr> <td width="12%" height="23">DATE</td> <td width="88%" height="23"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Month&nbsp; <SELECT size=1 name='month'> <OPTION value=January selected>January</OPTION> <OPTION value=February>February</OPTION> <OPTION value=March>March</OPTION> <OPTION value=April>April</OPTION> <OPTION value=May>May</OPTION> <OPTION value=June>June</OPTION> <OPTION value=July>July</OPTION> <OPTION value=August>August</OPTION> <OPTION value=September>September</OPTION> <OPTION value=October>October</OPTION> <OPTION value=November>November</OPTION> <OPTION value=December>December</OPTION> </SELECT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Date <SELECT size=1 name='date'> <OPTION value=1 selected>1</OPTION> <OPTION value=2>2</OPTION> <OPTION value=3>3</OPTION> <OPTION value=4>4</OPTION> <OPTION value=5>5</OPTION> <OPTION value=6>6</OPTION> <OPTION value=7>7</OPTION> <OPTION value=8>8</OPTION> <OPTION value=9>9</OPTION> <OPTION value=10>10</OPTION> <OPTION value=11>11</OPTION> <OPTION value=12>12</OPTION> <OPTION value=13>13</OPTION> <OPTION value=14>14</OPTION> <OPTION value=15>15</OPTION> <option value="16">16</option> <option value="17">17</option> <option value="18">18</option> <option value="19">19</option> <option value="20">20</option> <option value="21">21</option> <option value="22">22</option> <option value="23">23</option> <option value="24">24</option> <option value="25">25</option> <option value="26">26</option> <option value="27">27</option> <option value="28">28</option> <option value="29">29</option> <option value="30">30</option> <option value="31">31</option> </SELECT> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Year&nbsp; <SELECT size=1 name='year'> <OPTION value=2002 selected>2002</OPTION> <OPTION value=2003>2003</OPTION> <OPTION value=2004>2004</OPTION> <OPTION value=2005>2005</OPTION> <option value="2006">2006</option> <option value="2007">2007</option> <option value="2008">2008</option> <option value="2009">2009</option> <option value="2010">2010</option> </SELECT> </td> </tr> <tr> <td width="12%" height="39">TITLE</td> <td width="88%" height="39"> <p><input type="text" name="title" size="81"></p> </td> </tr> <tr> <td width="12%" height="19">MESSAGE</td> <td width="88%" height="19"> <textarea name="message" wrap= on rows="8" cols="70"> </textarea></td> </tr> </table> </td> </tr> </table> </td> </tr> </table> <input type="hidden" name="action" value="preview"> <p><input type="submit" value="Preview" name="B1"> <input type="reset" value="Reset" name="B2"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Place event at the&nbsp; BEGINING <input type="radio" value="beg" name="placement"> or the END <input type="radio" name="placement" value="end" checked> of existing events</p> </form> </body> </html>

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2022-12-09 12:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?