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

Hi Monks, I need to add a pop up confirmation box before deleting a record from database like "Yes/No" or "OK/Cancel". I found a thread here on perlmonk which is complicated for me as I am a very new in perl at http://www.perlmonks.org/?node_id=125561 If someone can help me do that will be a huge favor. Here is my script :
#!/usr/local/bin/perl use CGI; use DBI; # Make up a pulldown menu of all known patients $db_handle = DBI -> connect("DBI:Pg:dbname=northwind; host=localhost", "postgres", "postgres", {'RaiseError' => 1}); $query = "SELECT \"EmployeeID\" AS empid, \"FirstName\"::text || ' ' ||\"LastName\"::text AS name FROM \"Employees\""; $db_handle->do("SET search_path to northwind") or die; $qh = $db_handle->prepare($query); $qh->execute; while (@row = $qh->fetchrow) { $hh .= "<option value=$row[0]>$row[1]</option>\n"; } # Send out the header and form print "content-type: text/html\n\n"; print <<"HEADER"; <html> <head> <title>Delete an employee record</title> <body bgcolor=pink text=#3300CC border=2 bordercolor=pink > <h1 style="color:3300CC;">Delete an employee</h1> <style type="text/css"> .container { width: 500px; clear: both; } .container input { width: 100%; clear: both; } </style> </head> <div class="container"> <form method=POST> Select Employee name to delete :<select name=empid>$hh</select><br> <input type=submit name=go value=DELETE> </div> </form><hr> HEADER # Read information from the form read(STDIN,$buffer,$ENV{CONTENT_LENGTH}); @pairs = split(/&/,$buffer); foreach (@pairs) { ($n,$v) = split(/=/); $v =~ tr/+/ / ; $v =~ s/%(..)/pack("C",hex($1))/ge; $input{$n} = $v; } # If the form was properly submitted, save the data if ($input{"go"} eq "DELETE") { $query = "DELETE FROM \"Employees\" WHERE \"EmployeeID\"="." \'$input{empid}\'"; $db_handle -> do($query); $action = "Record saved - $query"; # If the form has not been submitted, ask for data } else { $action = "Please complete form"; } # Standard links to the rest of the application print <<"FOOTER"; <b>$action</b> <hr> Jump to - <a href=http://localhost/perlproj/cgi-bin/emp2.pl>View Emplo +yees Listing</a><br> Jump to - <a href=http://localhost/perlproj/cgi-bin/addemp.pl>Add an E +mployee</a><br> Jump to - <a href=http://localhost/perlproj/cgi-bin/updatephoto.pl>Add + or update Employee Photo</a><br> <hr> Edited by Terry on July, 06 2014. </body></html> FOOTER
Many tanks Terry

Replies are listed 'Best First'.
Re: Adding a pop up confirmation box
by poj (Abbot) on Jul 06, 2014 at 06:40 UTC
    Not perl but try
    <form method="post" onsubmit="return confirm('Do you really want to delete?');">
    poj
      Hi Poj, I added it to the code but its now resulting in error:
      # If the form was properly submitted, save the data if ($input{"go"} eq "DELETE") { <form method="post" onsubmit="return confirm('Do you really want to de +lete?');"> $query = "DELETE FROM \"Employees\" WHERE \"EmployeeID\"="." \'$input{empid}\'"; $db_handle -> do($query); $action = "Record saved - $query"; ....
      Error :
      Scalar found where operator expected at delemp.pl line 73, near "$quer +y" (Missing semicolon on previous line?) syntax error at delemp.pl line 73, near "$query "
      If I comment out your code script gets executed, did I put it on a wrong part of the script ? Rgds Terry

        As poj said, this:

        <form method="post" onsubmit="return confirm('Do you really want to de +lete?');">

        isn't Perl. You'd want to put this into your HTML, replacing this line:

        <form method=POST>

        Also see window.confirm on the MDN.

        Another option is to add a step with another form and hidden fields. Refactored it would look something like this.
        #!/usr/local/bin/perl use strict; use CGI; use CGI::Carp 'fatalsToBrowser'; #remove for prod use DBI; # get form parameters my $q = new CGI; my $action = $q->param('go'); my $empid = $q->param('empid'); my $dbh = dbh(); # connect to db $dbh->do("SET search_path to northwind") or die; # If the confirm form was properly submitted, delete the record my $msg; # change validation to suit if ( ($action eq "Confirm Delete") && ($empid =~ /\d+/)) { my $sql = qq! DELETE FROM "Employees" WHERE "EmployeeID" = ? !; my $count = $dbh->do( $sql,undef,$empid ); $msg = "$count Record deleted - $sql, $empid"; } else { $msg = "Please complete form"; } # get employees my $sql = qq!SELECT "EmployeeID" AS empid, "FirstName"::text || ' ' ||"LastName"::text AS name FROM "Employees" !; my $ar = $dbh->selectall_arrayref($sql); # Make up a pulldown menu my $options = qq!<option value="">select name</option>!; for my $row (@$ar) { $options .= qq!<option value="$row->[0]">$row->[1]</option>\n!; } # build html page my $style = q! body { background-color: pink ; color: #3300cc; } .container { width: 500px; clear: both; } .container input { width: 100%; clear: both;} !; # Send out the header and form print $q->header; print $q->start_html(-title=>'Delete an employee record', -style=>{ -code=>$style } ); print qq!<h1 style="color:3300CC">Delete an employee</h1>!; # confirm delete form if ( $action eq "DELETE" ) { print qq!<h3>Are you sure you want to delete $empid ?</h3> <form action="" method="post"> <input type="hidden" name="empid" value="$empid"/> <input type="submit" name="go" value="Confirm Delete"/> <input type="submit" name="go" value="Cancel"/> </form>!; } else { print qq!<div class="container"> Select Employee name to delete : <form method="post" action=""> <select name="empid"> $options </select><br/> <input type="submit" name="go" value="DELETE"/> </form></div><hr/>!; # Standard links to the rest of the application print <<"FOOTER"; <b>$msg</b> <hr/> Jump to - <a href="emp2.pl">View Employees Listing</a><br/> Jump to - <a href="addemp.pl">Add an Employee</a><br/> Jump to - <a href="updatephoto.pl">Add or update Employee Photo</a><br +/> <hr/> Edited by Terry on July, 06 2014. FOOTER } print $q->end_html; # connect to database sub dbh { my $dsn = 'DBI:Pg:dbname=northwind;host=localhost'; my $user = '***'; my $pwd = '***'; my $dbh = DBI -> connect($dsn,$user,$pwd,{'RaiseError' => 1}); return $dbh; }
        poj
Re: Adding a pop up confirmation box
by kcott (Archbishop) on Jul 06, 2014 at 07:33 UTC
      Hi, I added the following code to the script and its not working can you please help me correct it.
      print header; print <<EOF; <script type="text/javascript"> function confirmOk() { return window.confirm("Confirm deletion?"); } </script> EOF
      And
      # If the form was properly submitted, save the data if ($input{"go"} eq "DELETE") { print button(-name=>'Delete', -value=>'DO you really want to delete it ?', -onClick=>"return confirmOk()"); $query = "DELETE ....
      Rgds Terry
Re: Adding a pop up confirmation box
by Anonymous Monk on Jul 06, 2014 at 07:31 UTC