Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Editing Posted Data

by ejx2 (Initiate)
on Jul 08, 2005 at 02:33 UTC ( [id://473308]=perlquestion: print w/replies, xml ) Need Help??

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

I'm very new to perl, infact this is my first accomplishment so far. So, please bear with me while I try to learn from the best.

My question is how can I change this script to allow my users to edit the data they posted? For example if one of my analysts forget to add a comment or make a typo, I'd like them to be able to click a link, on the page that contains the post, edit the content and then click "save" or "resubmit".

#!/usr/bin/perl -w use strict; use CGI qw(:standard); use DBI; print header, start_html("Daily Communication Log"); print "<a href = /form.html>Submit New Log Entry</a><br><br>"; # Declare variables my ( $analyst, # name of the analyst making the entry $name, # name from combo box on html form $shift, # shift analyst is on $cust, # new customer turn up $issues, # customer specific issues analysts should be aware of $eoi, # events that require observation $message, # general comments not related to previous fields $cur, # keeps current entries $dbh, # database handler $filter, # escapes single quote $sql, # handles statements $stq, # does query ); $cur = CGI->new(); #hold current request $message=$cur->param("message"); $cust=$cur->param("cust"); $issues=$cur->param("issues"); $eoi=$cur->param("eoi"); $analyst=$cur->param("analyst"); ($shift,$name)=split(/:/,$analyst); # Connect to Database $dbh = DBI->connect("DBI:mysql:dcl:localhost","user","passwd"); if ($message) { # SQL Statement $stq = $dbh->prepare( qq{ insert into dcl_log values(now(),"$name","$s +hift","$cust","$issues","$eoi","$message") }); $stq->execute || print "Unable to execute MySQL statement"; } # Execute query and print after values have been entered # Need to see the last 5 entires in reverse order $sql = "select * from dcl_log order by timestamp desc limit 5"; $stq = $dbh->prepare($sql); $stq->execute || print "Could not complete SQL statement ... check syntax." +; # Output results my @row; while (@row=$stq->fetchrow_array) {table_data (@row);} $dbh->disconnect(); print end_html; sub table_data { my @data = @_; my %D; print "<center><table border=0 width=100%>"; $D{'time'} = $data[0]; $D{'name'} = $data[1]; $D{'shift'} = $data[2]; $D{'cust'} = $data[3]; $D{'issues'} = $data[4]; $D{'eoi'} = $data[5]; $D{'message'} = $data[6]; print "<tr><td colspan=2 align=center><b>$D{'time'}</b></td></tr>\n"; print "<tr><td width=1% align=left><b>Name:</b>\n"; print "<td align=left>$D{'name'}</td></tr>\n"; print "<tr><td align=left><b>Shift:</b>\n"; print "<td align=left>$D{'shift'}</td></tr>\n"; print "<tr><td align=left><b>Customer:</b></td></tr>\n"; print "<tr><td colspan=2 align=left>$D{'cust'}</td></tr>\n"; print "<tr><td align=left><b>Issues:</b></td></tr>\n"; print "<tr><td colspan=2 align=left>$D{'issues'}</td></tr>\n"; print "<tr><td align=left><b>EOI:</b></td></tr>\n"; print "<tr><td colspan=2 align=left>$D{'eoi'}</td></tr>\n"; print "<tr><td align=left><b>Message:</b></td></tr>\n"; print "<tr><td colspan=2 align=left>$D{'message'}</td></tr>\n"; print "</table></center><hr>\n"; }

janitored by ybiC: Balanced <readmore> tags around longish codeblock, as per Monastery convention

Replies are listed 'Best First'.
Re: Editing Posted Data
by Tanktalus (Canon) on Jul 08, 2005 at 03:15 UTC

    I think I'd start with your question in HTML before worrying about how to get the perl code to do what you want. What do you want the page to look like? There are many ways to do what you're asking, so a bit more specific would be useful. Do you want the user to click on a link to go to an editable version of the page (href anchor), or do you want this page to be editable directly (rather than putting it in a td element, put the whole table in a form, and put your input element inside the td element, with a default value of the current value)?

    Then I'd go on to cleanup. ;-) You don't need the qw(:standard) for your use CGI line if you are using the object-oriented version of CGI. Or, if you're using the procedural version, don't use the object version. It's just a bit confusing.

    I'd then suggest going with some sort of templating solution to get rid of all that HTML embeded in your perl code. Even if you're worried about speed, it can often be faster to load a template than to have it compiled in. But, premature optimisations aside, it's also easier to read. Both the code and the template.

    Then I would suggest the whole thing could use a significant paradigm shift by looking into CGI::Application. The flexibility you'll get going that way may just amaze you. It did for me.

Re: Editing Posted Data
by neniro (Priest) on Jul 08, 2005 at 08:07 UTC
    First off all I suggest you to use HTML::Template or one of the other popular templating systems to separate code from content. You print a table for each record you receive - maybe it would be better to print just new table-rows for each of them. If you want your users to be able to edit those recordsets again you could create a html-form for each recordset, display them in input-tags and add a submit button. Based on the action you receive via CGI your script will display/add or edit records.

    UPDATE:

    An example CGI app of my own to show you a way to structure such an app:

    #!/usr/bin/perl use strict; use warnings; use CGI; use CGI::Carp qw ( warningsToBrowser fatalsToBrowser ); use DB::Countries; use Templating::Countries; use Query::Countries; # # declaration # my $q = new CGI; my $form = $q->Vars(); my $db = new DB::Countries; my $tmpl = new Templating::Countries; my $dispatch = { 'search' => sub { my $query = new Query::Countries ($form); $tmpl->overview($db->get($query)); }, 'edit' => sub { $tmpl->edit($db->get($form->{'id'})); }, 'new' => sub { $tmpl->add(); }, 'delete' => sub { $tmpl->del($db->get($form->{'id'})); }, 'update' => sub { $db->update($form); $tmpl->default(); }, 'insert' => sub { $db->insert($form); $tmpl->default(); }, 'remove' => sub { $db->remove($form->{'ID_country'}); $tmpl->default(); }, 'default' => sub { $tmpl->default() }, }; # # main # print $q->header(); print $dispatch->{$form->{action}} ? $dispatch->{$form->{action}}() : $dispatch->{'default'}(); exit;
    You can see that the app is reduced to a dispatch and just a few lines of code. Everything else is seperated in modules. One for all database-action, one for building queries and one for the templating. Based on the $form->action it receives it calls the related actions using the dispatch table.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2024-04-23 11:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found