Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 1st, 2012  |  syntax: None  |  size: 4.57 KB  |  hits: 8  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Retrieivng mysql data using PERL and jquery-ajax
  2. <!DOCTYPE html>
  3. <html>
  4.     <head>
  5.         <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js" charset="utf-8"></script>
  6.         <script type="text/javascript" class="jsbin" src="C:/xampp/DataTables/media/js/jquery.dataTables.js" charset="utf-8"></script>
  7.         <script type="text/javascript" src="js/main.js" charset="utf-8"></script>
  8.         <script type="text/javascript" src="js/RunPerlScript.js" charset="utf-8"></script>
  9.         <script type="text/javascript" src="js/table.js" charset="utf-8"></script>
  10.     </head>
  11.     <body>
  12.         <header>
  13.             <h1></h1>
  14.         </header>
  15.         <form name="myForm" method="GET" action="">
  16.             <select id="cdLDAP" >
  17.                 <option/>
  18.             </select>
  19.             <input type="button"  id="btn_run" name="btn_run" value="Run"></input>
  20.         </form>
  21.         <table id="results_table">
  22.             <thead>
  23.                 <tr>
  24.                     <th></th>
  25.                 </tr>
  26.             </thead>
  27.             <tbody>
  28.                 <tr>
  29.                     <td></td>
  30.                 </tr>
  31.                 <tr>
  32.                     <td></td>
  33.                 </tr>
  34.             </tbody>
  35.         </table>
  36.     </body>
  37. </html>
  38.        
  39. #!/usr/bin/perl -T
  40.     use CGI;
  41.     use DBI;
  42.     use strict;
  43.     use warnings;
  44.  
  45.     # read the CGI params
  46.     my $cgi = CGI->new;
  47.     my $inputselection = $cgi->param("cdLDAP");
  48.     my $html_table = $cgi->param("html_table_results");
  49.     my ($base_dn, $tblname);
  50.     #my $password = $cgi->param("password");
  51.  
  52.     #my $inputselection = "Disabled Users";
  53.     #LDAP Connection parameters
  54.     if ($inputselection eq "Disabled Users") {
  55.         $base_dn = "";
  56.         $tblname = "disabled_user";
  57.     } elsif ($inputselection eq "") {
  58.         $base_dn = "";
  59.         $tblname = "service_account";
  60.     } elsif ($inputselection eq "") {
  61.         $base_dn = "";
  62.         $tblname = "";
  63.     } else {
  64.         die;
  65.     }
  66.  
  67.     # connect to the database
  68.     my ($platform,$database,$host,$port,$db_user,$pw) = ("mysql","results","localhost","3306","resultsuser","mysql123");
  69.     my $dsn = "DBI:$platform:database=$database,host=$host,port=$port";
  70.     my $connect = DBI->connect("DBI:mysql:database=$database;host=$host",$db_user,$pw,{RaiseError => 1});
  71.  
  72.     #query db to get results set for table output
  73.     my $query_results = "SELECT * FROM " . $tblname;
  74.     my $query_handle = "";
  75.     $query_handle = $connect->prepare($query_results) or die $connect->errstr;
  76.     $query_handle->execute() or die $query_handle->errstr;
  77.  
  78.     print header;
  79.     # HTML for the beginning of the table
  80.     # we are putting a border around the table for effect
  81.     print "<table border="1" width="800"> n";
  82.  
  83.     # print your table column headers
  84.     print "<tr><td>User ID</td><td>Status</td><td>Last Password Reset</td><td>Reset Needed?</td></tr>n";
  85.  
  86.     my (@data,$uid,$status,$pwlstset,$resetmsg);
  87.  
  88.     # retrieve the values returned from executing your SQL statement
  89.     foreach (@data = $query_handle->fetchrow_array()) {
  90.     $uid = $data[0];
  91.     $status = $data[1];
  92.     $pwlstset = $data[2];
  93.     $resetmsg = $data[3];
  94.  
  95.     # print your table rows
  96.     print "<tr><td>$uid</td><td>$status</td><td>$pwlstset</td><td>$resetmsg</td></tr>n";
  97.  
  98.     }
  99.  
  100.     # close your table
  101.     print "</table>n";
  102.  
  103.     # exit the script
  104.     exit;
  105.        
  106. $(function() {
  107.     $('#btn_run').click(function() {
  108.         var tblname = $('#cdLDAP').val();
  109.         var html_table = $('#results_table').attr('id');
  110.         $.ajax({
  111.             type: "GET",
  112.             url: "/perl/cgitest.pl", // URL of the Perl script that queries LDPA and inputs to mySQL
  113.             data: "cdLDAP=" +tblname,
  114.             // script call was *not* successful
  115.             error: function() {
  116.                 alert("ERROR!");
  117.             }, // error
  118.             // script call was successful
  119.             // data contains the JSON values returned by the Perl script
  120.             success: function(data){
  121.                 alert("success!");
  122.             } // success
  123.         }); // ajax
  124.         $.ajax({
  125.             type: "GET",
  126.             url: "/perl/cgitest2.pl", // URL of the Perl script that retirves data from mySQL
  127.             data: "cdLDAP=" +tblname +",html_table_results=" +html_table,
  128.             // script call was *not* successful
  129.             error: function() {
  130.                 alert("ERROR!");
  131.             }, // error
  132.             // script call was successful
  133.             // data contains the JSON values returned by the Perl script
  134.             success: function(data){
  135.                 alert("success!");
  136.             } // success
  137.         }); // ajax
  138.     });
  139. });