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

Hello I am very new to writting perl scripts and I want to write a script to set the variables for another script. Here is basically what I want to do:

1). I want to have a form page that a person can enter the valuables to.

Example of the form:

<form method=post action=test.cgi> Server Address: <input type=text name="server_address"> Cgi Directory: <input type=text name="cgi_directory"> <input type=submit value=save> </form>

2). Then I want the form's submission to be saved to a file called test.cfg in this form:

$server_address = 'http://www.whatever.com';
$cgi_directory = '/InetPub/wwwroot/cgi-bin';

3). Then when the person wants to change the information, I want them to be able to go back to the form and have the form read in the input fields what they already saved.

4). Then when they change the information I only want the information that they changed to be updates in the test.cfg file.

I know this can be done, but I cant figure it out, the closets I have came to was getting the information from the form to the test.cfg file.

An example of this would be the Ultimate Bulletin Board. Any suggestions and help would be gratefully appreciated.

Thanks in advance.
Jonathan

Replies are listed 'Best First'.
Re: Form/File/Form
by vroom (His Eminence) on Apr 21, 2000 at 07:44 UTC
    use CGI; my $query=CGI::new(); my %config; my @params; if($query->param('submit') eq "save"){ #if we want to save new parameters open log file #and read old settings into config hash if("test.cfg" -e){ open FILE, "<test.cfg" || die "Can't open $!"; while(<FILE>){ if(/(.*)=(.*)/){ $config{$1}=$2; } } close FILE; } @params=$query->param(); #add/overwrite params just submitted in config hash foreach(@params){ $config{$_}=$query->param($_) unless $_ eq "submit"; } #write all the config key/value pairs out to the file open FILE, ">test.cfg" || die "Can't open! $!"; foreach(keys %config){ print FILE "$_=$config{$_}\n"; } close FILE; }
    This should work although I haven't tested it.

    vroom | Tim Vroom | vroom@cs.hope.edu
Re: Form/File/Form
by jmoore (Initiate) on Apr 21, 2000 at 08:59 UTC
    Would this take care of the whole cgi script? In other words do I use this code in place of my other code?
      As far as I understand what you want to do this should take care of it.
Re: Form/File/Form
by jmoore (Initiate) on Apr 21, 2000 at 09:05 UTC
    Ok I figured out one thing, now I have another question. How do I get a form on an HTML document to remember what was submitted before?
      If I understand the question;
      If the user fills in the field: username with the response "Chuck" you want to update the file with that information.
      You then want to display the screen again with that information still in place?
      if that's the case, the way I have done it in the (distant (about 2 years ago))past was to rebuild the html in the cgi script using data from the file to fill in the form defaults.
      Hope that helps.

      Your humble servant,
      -Chuck
      Or use cookies. But the solution above is probably easier.
        Ok here is what I mean by having a form remember what was typed into it.

        First a person fills out a form and it saves the information into a text database (IE. Configureration Form). Then they decide that they want to change one thing in the database file, so they go back to the form. At this point when they look at the form they see what they entered before already filled into the form so now all they have to do is to change that one field on the form. Then the form updates the database with the new settings.

        Does that make since? I seen this done before but I have had no clue on how they done it. Also like I said before, I am very new at writing perl scripts and I am now just starting to learn. Basically I just gathered scripts that does somewhat what I need and then change it to what I need. Shoot I really dont even know how to write a perl script that saves to a file.. lol. But I am a fast learner, my first script that I ever built was a simple form to math script (like a shopping cart).

        Thanks in advance...