Working With AJAX and Perl with example
Hello,
While doing Ajax Perl application development, I came across an interesting article on the following link.
The article demonstrate with an example how Ajax can be used with Perl. In the example given, there is table containing Student names and Marks. Every row has an Edit button, by which user can edit the information for that row. THE DB used is Microsoft Access to keep things simple. In order to run the below example code you will have to create a table by the name “Student” in MS Access”. It should have the following columns.
Column Name Data type Sl_No(Primary Key) Number Name Text Marks Number
Also you need to create a DSN by the name “mydsn” pointing to the Access DB.
The basic logic here used in the example is that we have two separate rows for View and Edit (for every Student). Initially we hide the Edit row (by using style="display:none") and display the View Row(by using style="display:block"). When the user clicks on the “Edit” button, the View row becomes hidden and the Edit row is displayed. We have used JavaScript to toggle between the rows. Below is the code snippet used in files (AjaxExample.pl and student.js).
============================================================
AjaxExample.pl
============================================================
#!perl
use DBI qw(:sql_types);
use CGI;
use CGI qw/:standard/;
use CGI::Ajax;
my $cgi = new CGI();
#——————————————————————–
# Mapping the Perl subroutine to the triggering function
#——————————————————————–
my $ajax = new CGI::Ajax( 'saveStudInfo_JScript' => &saveStudInfo_PerlScript );
$cgi->header(-charset=>'US-ASCII');
print $ajax->build_html($cgi,&generateHTML);
#——————————————————————–
# Subroutine which generates the HTML
#——————————————————————–
sub generateHTML {
# Hash which contains existing data
%Students = get_Student_Info();
$cnt = 1;
# Write the html code in the form of a string
$returnHTML .= "n";</span>
$returnHTML .= "n";</span>
$returnHTML .= "n
$returnHTML .= "";
$returnHTML .= "n</HEAD>";
$returnHTML .= "n";</span>
$returnHTML .= "<FORM method="POST" enctype="multipart/form-data" name="StudentForm">";
$returnHTML .= "n
$returnHTML .= "n
$returnHTML .= "n<TABLE cellspacing="0″ cellpadding="0″ align="center">";
$returnHTML .= "n
$returnHTML .= "n<TABLE width="100%" align="center" border="1″ cellpadding="3″ cellspacing="1″ id= "StudentInfoTable">";
$returnHTML .= "n
$returnHTML .= "n
$returnHTML .= "n<TD align="center" nowrap>Name </TD>";
$returnHTML .= "n
$returnHTML .= "n
$returnHTML .= "n</TR >";
foreach $key (sort { $a <=> $b }(keys %Students)) {
#View row
$returnHTML .= "n";
$returnHTML .= "n
$returnHTML .="n
$returnHTML .="n<TD align="center" nowrap>
$returnHTML .="n<TD align="center" nowrap>
$returnHTML .= "n<TD align="center"></TD>";
$returnHTML .= "n</TR >";
#Edit row
$returnHTML .= "n
";
";
";</span>
";</span>
SL No ";
Marks ";
";
";</span>
$key ";