开发者

HTML Perl update button with SQL problem

开发者 https://www.devze.com 2023-02-15 19:17 出处:网络
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).

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 curr开发者_如何学编程ent 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.


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>';


I am not sure why you want to do it this way. But I would do it

  • Create one script which does all the copy stuff keep it on server.
  • Create a web page with XMLHttpRequest in its submit button which inturn will be calling the original script doing the sql operation.
  • Capture the responseText response and print the message on your page using js, or put one alert box there.

Simpler solution is:

  • Create a script on server which has the submit button and one hidden input which can tell about the next operation
  • based on the hidden parameter call your sub available in same script as sub.
  • capture the response and do what ever java script magic you want to do. If you need I can provide a sample too for second approach.
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号