Reputation: 45
I'm trying to create a sort of update button, when a user clicks on a HTML Perl button it asks for confirmation (yes/no).
If the user clicks "Yes" it runs a SQL script then returns a pop-up (JavaScript) saying operation successful (or not).
The SQL code is creating a backup table then copying the current values stored in the database to this backup table, so no screen refresh is necessary. The complete and functioning code is provided below:
CREATE TABLE IF NOT EXISTS backup_tab_right_mapping
LIKE tab_right_mapping;
DELETE FROM backup_tab_right_mapping
WHERE group_id = "1"
AND role_id = "1";
INSERT INTO backup_tab_right_mapping
SELECT * FROM tab_right_mapping
WHERE group_id = "1"
AND role_id = "1";
However I dont want to use PHP, I'm looking for a Sub solution.
Thanks for your time and any help you can provide XD
EDIT: Project requirements only allow me to use perl, HTML and JavaScript.
Upvotes: 1
Views: 694
Reputation: 39158
Works also without JavaScript. You need to add more client-side error-checking in case the program aborts abnormally.
To follow best practice, store the database credentials outside the program.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"><head><title />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js" />
</head><body><form action="backup-table" method="POST"><input type="submit" /></form>
<script>
$('form').submit(function() {
if (confirm('Backup table?')) {
$.post("backup-table", function(data) {
alert($(data).text());
});
return false;
};
});
</script>
</body></html>
#!/usr/bin/perl -T
use strict;
use warnings FATAL => 'all';
use CGI qw();
use DBI qw();
my $dbh = DBI->connect('dbi:…', '…username…', '…password…', {RaiseError => 1, AutoCommit => 1,});
for my $sql (
'CREATE TABLE IF NOT EXISTS backup_tab_right_mapping
LIKE tab_right_mapping;',
'DELETE FROM backup_tab_right_mapping
WHERE group_id = "1"
AND role_id = "1";',
'INSERT INTO backup_tab_right_mapping
SELECT * FROM tab_right_mapping
WHERE group_id = "1"
AND role_id = "1";',
) {
$dbh->do($sql);
}
my $cgi = CGI->new;
print $cgi->header('application/xhtml+xml');
print '<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"><head><title /></head>
<body><p>Backup complete</p></body></html>';
Upvotes: 2
Reputation: 301
I am not sure why you want to do it this way. But I would do it
Simpler solution is:
Upvotes: 0