Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Re: Add,Insert,Change,Delete File / Array

by BigJoe (Curate)
on Oct 04, 2001 at 17:51 UTC ( #116703=note: print w/ replies, xml ) Need Help??


in reply to Re: Add,Insert,Change,Delete File / Array
in thread Add,Insert,Change,Delete File / Array

This is a quick and untested example of how to use CGI in your script. There is alot more that it can do for you also.

#!/usr/bin/perl use strict; use CGI; my $q = new CGI; #VARIABLES TO CHANGE ############################ $scriptfile = 'test.cgi'; #Name of this script $datafile = 'testdata.dat'; #Name of the file to sto +re data to $workdir = 'http://myworld.server101.com'; #Default Working Dir #OTHER VARIABLES ############################ $action = $q->param('action'); $userid = $q->param('userid'); $name1 = $q->param('name1'); $name2 = $q->param('name2'); $line = $q->param('line'); #DECIDE ACTION TO TAKE ############################ if ($action eq '') { &Show;} if ($action eq 'show') { &Show;} if ($action eq 'add') { &Add;} if ($action eq 'addsave') { &Addsave;} if ($action eq 'change') { &Change;} if ($action eq 'changesave'){ &Changesave;} if ($action eq 'insert') { &Insert;} if ($action eq 'insertsave'){ &Insertsave;} if ($action eq 'delete') { &Delete;} #ADD - Create Form ############################ sub Add { print $q->header; print qq| <form action=$scriptfile> <input type=hidden name=action value=addsave> ForeName:<input type=text name=name1 size=20><br> Surname :<input type=text name=name2 size=20><br> <input type=submit value="Add Record"> </form> |; exit; } #ADD - Save data to File ############################ sub Addsave { $newname = join('', $name1, $name2); $newname .= "\n"; open ("database", ">>$datafile") || &Printerror; print database $newname; close(database); print "Location: $workdir/cgi/$scriptfile?action=show\n\n"; exit; } #CHANGE - Create Form ############################ sub Change { print $q->header; open ("database", "$datafile") || &Printerror; @database = <database>; close(database); ($name1, $name2) = split ('[]',@database[$line]); $name2 =~ s/\n//g; print qq| <form action=$scriptfile> <input type=hidden name=action value=changesave> <input type=hidden name=line value=$line> Forename:<input type=text name=name1 size=20 value=$name1><br> Surname :<input type=text name=name2 size=20 value=$name2><br> <input type=submit value="Change Record"> </form> |; exit; } #CHANGE - Save changed data to File ################################### sub Changesave { $newname = join('', $name1, $name2); $newname .= "\n"; open ("oldbase", "$datafile") || &Printerror; @oldbase = <oldbase>; close(oldbase); @oldbase[$line] = $newname; open ("newbase", ">$datafile") || &Printerror; print newbase @oldbase; close(newbase); print "Location: $workdir/cgi/$scriptfile?action=show\n\n"; exit; } #INSERT - Create Form ############################ sub Insert { print $q->header; print qq| <form action=$scriptfile> <input type=hidden name=action value=insertsave> <input type=hidden name=line value=$line> Forename:<input type=text name=name1 size=20><br> Surname :<input type=text name=name2 size=20><br> <input type=submit value="Insert Record"> </form> |; exit; } #INSERT - Save inserted data to File ##################################### sub Insertsave { $newname = join('', $name1, $name2); $newname .= "\n"; open ("oldbase", "$datafile") || &Printerror; @oldbase = <oldbase>; close(oldbase); $oldname = @oldbase[$line]; @oldbase[$line] = "$newname$oldname"; open ("newbase", ">$datafile") || &Printerror; print newbase @oldbase; close(newbase); print "Location: $workdir/cgi/$scriptfile?action=show\n\n"; exit; } #DELETE - delete record ################################### sub Delete { open ("oldbase", "$datafile") || &Printerror; @oldbase = <oldbase>; close(oldbase); @oldbase[$line] = ''; open ("newbase", ">$datafile") || &Printerror; print newbase @oldbase; close(newbase); print "Location: $workdir/cgi/$scriptfile?action=show\n\n"; exit; } #SHOW - Create List of Records ############################## sub Show { $line = "-1"; print $q->header; open ("database","$datafile")|| &Printerror; @database = <database>; close(database); foreach (@database) { $line += 1; ($name1, $name2) = split ('[]',$_); print qq| $line $name1 $name2 \| <a href=$scriptfile?action=change&line=$line>Edit</a> \| <a href=$scriptfile?action=delete&line=$line>Delete</a> \| <a href=$scriptfile?action=insert&line=$line>Insert</a><br>|; } print qq| <br> <a href=$scriptfile?action=add>Add Record</a> |; exit;


--BigJoe

Learn patience, you must.
Young PerlMonk, craves Not these things.
Use the source Luke.


Comment on Re: Re: Add,Insert,Change,Delete File / Array
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://116703]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (14)
As of 2014-10-21 18:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (106 votes), past polls