Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loading Perl vars into JavaScript 2

Status
Not open for further replies.

tsp120

Programmer
May 21, 2003
52
US
I setting up a webpage. The page is written in Perl/CGI but uses JavaScript for a variety of reasons.

When the page loads, I would like to perform an SQL statement to find all records in a table and store each tuple into an array.

For example, if I had a table with the following columns:
id - name - email

and had the following info:
1 - Jack - jack@zzz.com
2 - John - john@zzz.com
3 - Mary - mary@zzz.com

I would like these JavaScript arrays to be populated when the page loads:

idArr[0] = 1
idArr[1] = 2
idArr[2] = 3

nameArr[0] = Jack
nameArr[1] = John
nameArr[2] = Mary

emailArr[0] = jack@zzz.com
emailArr[1] = john@zzz.com
emailArr[2] = mary@zzz.com

How could I do this?

TIA
 
Code:
my @ids = ();
my @names = ();
my @emails = ();

# get the data from the database
# and push each item into each
# array...

# write your javascripts
# using for loops to define
# each array but using the
# same index numbers for each
# bit of related data
 
Code:
use DBI;
my $dbh = DBI->connect($db, $user, $pass) or die "Couldn't connect to database: " . DBI->errstr;
my $sth= $dbh->prepare('select id, name, email from table');
$sth->execute()
while (@array=$sth->fetchrow_array) {
  push (@ids, $array[0]);
  push (@name, $array[1]);
  push (@email, $array[2]);
}
$sth->finish;
$dbh->disconnect;
#code to output javascript here
use CGI;
$q=new CGI;
print $q->header(-type=>'text/javascript');
#do what you gotta here

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top