【问题标题】:Retrieivng mysql data using PERL and jquery-ajax使用 PERL 和 jquery-ajax 检索 mysql 数据
【发布时间】:2012-05-02 04:32:51
【问题描述】:

我正在创建一个包含 3 个基本元素、选择框、按钮和表格的网页。从较高的层次来看,用户选择一个元素,然后单击按钮。单击该按钮时,将执行一个 PERL 脚本,将数据放入 mySQL 数据库中的表中。所有这些都成功了。

现在,我正在尝试使用动态表将 DB 表值返回到我的 HTML 文件中。但是我找到的每个来源都有我的 CGI 文件编写 html 标签。我觉得这是不对的,因为我不明白我的 CGI 知道表 ID,即使我传递了值。另外,我知道我的 js 文件不正确,有两个单独的 AJAX 调用,但那是我在脑海中逻辑处理的家。

我不确定先修复 CGI 还是 JS 文件。

HTML 代码(index.html):

<!DOCTYPE html>
<html>
    <head>
        <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js" charset="utf-8"></script>
        <script type="text/javascript" class="jsbin" src="C:/xampp/DataTables/media/js/jquery.dataTables.js" charset="utf-8"></script>
        <script type="text/javascript" src="js/main.js" charset="utf-8"></script>
        <script type="text/javascript" src="js/RunPerlScript.js" charset="utf-8"></script>
        <script type="text/javascript" src="js/table.js" charset="utf-8"></script>
    </head>
    <body>
        <header>
            <h1></h1>
        </header>
        <form name="myForm" method="GET" action="">
            <select id="cdLDAP" >
                <option/>
            </select>
            <input type="button"  id="btn_run" name="btn_run" value="Run"></input>
        </form>
        <table id="results_table">
            <thead>
                <tr>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td></td>
                </tr>
                <tr>
                    <td></td>
                </tr>
            </tbody>
        </table>
    </body>
</html>

Perl (CGI):

 #!/usr/bin/perl -T
    use CGI;
    use DBI;
    use strict;
    use warnings;

    # read the CGI params
    my $cgi = CGI->new;
    my $inputselection = $cgi->param("cdLDAP");
    my $html_table = $cgi->param("html_table_results");
    my ($base_dn, $tblname);
    #my $password = $cgi->param("password");

    #my $inputselection = "Disabled Users";
    #LDAP Connection parameters
    if ($inputselection eq "Disabled Users") {
        $base_dn = "";
        $tblname = "disabled_user";
    } elsif ($inputselection eq "") {
        $base_dn = "";
        $tblname = "service_account";
    } elsif ($inputselection eq "") {
        $base_dn = "";
        $tblname = "";
    } else {
        die;
    }

    # connect to the database
    my ($platform,$database,$host,$port,$db_user,$pw) = ("mysql","results","localhost","3306","resultsuser","mysql123");
    my $dsn = "DBI:$platform:database=$database,host=$host,port=$port";
    my $connect = DBI->connect("DBI:mysql:database=$database;host=$host",$db_user,$pw,{RaiseError => 1});

    #query db to get results set for table output
    my $query_results = "SELECT * FROM " . $tblname;
    my $query_handle = "";
    $query_handle = $connect->prepare($query_results) or die $connect->errstr;
    $query_handle->execute() or die $query_handle->errstr;

    print header;
    # HTML for the beginning of the table
    # we are putting a border around the table for effect
    print "<table border=\"1\" width=\"800\"> \n";

    # print your table column headers
    print "<tr><td>User ID</td><td>Status</td><td>Last Password Reset</td><td>Reset Needed?</td></tr>\n";

    my (@data,$uid,$status,$pwlstset,$resetmsg);

    # retrieve the values returned from executing your SQL statement
    foreach (@data = $query_handle->fetchrow_array()) {
    $uid = $data[0];
    $status = $data[1];
    $pwlstset = $data[2];
    $resetmsg = $data[3];

    # print your table rows
    print "<tr><td>$uid</td><td>$status</td><td>$pwlstset</td><td>$resetmsg</td></tr>\n";

    }

    # close your table
    print "</table>\n";

    # exit the script
    exit;

JS/JQuery/AJAX:

$(function() {
    $('#btn_run').click(function() {
        var tblname = $('#cdLDAP').val();
        var html_table = $('#results_table').attr('id');
        $.ajax({
            type: "GET",
            url: "/perl/cgitest.pl", // URL of the Perl script that queries LDPA and inputs to mySQL
            data: "cdLDAP=" +tblname,
            // script call was *not* successful
            error: function() { 
                alert("ERROR!");
            }, // error 
            // script call was successful 
            // data contains the JSON values returned by the Perl script 
            success: function(data){
                alert("success!");
            } // success
        }); // ajax
        $.ajax({
            type: "GET",
            url: "/perl/cgitest2.pl", // URL of the Perl script that retirves data from mySQL
            data: "cdLDAP=" +tblname +",html_table_results=" +html_table,
            // script call was *not* successful
            error: function() { 
                alert("ERROR!");
            }, // error 
            // script call was successful 
            // data contains the JSON values returned by the Perl script 
            success: function(data){
                alert("success!");
            } // success
        }); // ajax
    });
});

所以我需要一些帮助: 1) 我的 PERL 脚本是否应该编写 html 标签?如果是,我如何写入index.html 而不是新的 html 文件? 2)如果表结构是由jquery文件创建的,一个好的教学资源将不胜感激,因为我试图被教如何钓鱼,而不是给鱼。

【问题讨论】:

    标签: mysql perl html jquery


    【解决方案1】:

    我的 PERL 脚本应该编写 html 标签吗?

    no such thing as PERL

    如果您想使用 Ajax 动态更新页面(注意您应该使用unobtrusive JavaScript),那么您可以采用两种基本方法:

    1. 服务器以干净的数据结构(可能是 JSON)返回数据
    2. 服务器返回 HTML 片段

    我通常赞成前一种方法(在这种情况下,您将在 Perl 中构造一个 hashrefs 数组,通过a JSON module 运行它,然后使用application/json Content-Type HTTP 标头输出它。

    鉴于某些版本的 Internet Explorer 在尝试编辑表格元素的 innerHTML 时存在问题,这使得第一个选项更好。

    如果是,如何写入 index.html 而不是新的 html 文件?

    您根本不写入文件。您通过 STDOUT 返回数据,网络服务器将其传递回客户端。

    如果表结构是由jquery文件创建的,一个好的教学资源将不胜感激

    http://api.jquery.com/category/manipulation/

    【讨论】:

    • 我对 json 不熟悉,但这会起作用还是我还需要对其进行编码? my %json_hash = (); foreach (@data = $query_handle-&gt;fetchrow_array()) { %json_hash = ( "key" =&gt; "User ID", "val" =&gt; $data[0], "key" =&gt; "Status", "val" =&gt; $data[1], "key" =&gt; "Last Password Reset", "val" =&gt; $data[2], "key" =&gt; "Reset Needed", "val" =&gt; $data[3] ) } 这会为给定条目创建一个完整的哈希还是只创建一个并不断被清除?另外,从encode 文档中,通过将其创建为哈希,我不必使用encode 正确的假设吗?
    • 暂时将 JSON 放在一边,它不能作为 Perl 数据结构工作。我不明白您为什么使用“key”和“val”作为键名。您当然不能在单个哈希中多次使用相同的密钥。您还将在每次遍历循环时覆盖%json_hash,因为您对它什么都不做(例如,将对它的引用附加到数组中)。对于 JSON,您需要将 Perl 数据结构转换为 JSON(或其他基于字符串的序列化),然后再通过网络发送。
    • 好的,所以暂时忘记 JSON,因为我知道我将不得不 encode. 我想我修复了我的哈希,但我可以像我一样附加它:foreach (@data = $query_handle-&gt;fetchrow_array()) { %json_hash = ( %json_hash, "User ID" =&gt; $data[0], "Status" =&gt; $data[1], "Last Password Reset" =&gt; $data[2], "Reset Needed" =&gt; $data[3] )}TY Quentin
    • 现在你正在用它所拥有的任何内容的副本以及一些重复的键来覆盖散列。你不能有重复的键。您需要在数组中存储对哈希的引用。
    猜你喜欢
    • 1970-01-01
    • 2013-05-18
    • 2012-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多