Gossamer Forum
Home : Products : DBMan : Discussions :

Calling a custom dbman routine from JavaScript

Quote Reply
Calling a custom dbman routine from JavaScript
Hi, everyone!

I wish to call a Perl subroutine from JavaScript.

I already do this with routines that are in <a href=""> tags and <input type="button" onClick=""> calls. However, the routine that I wish to call is part of a process logic flow, i.e., an event that is fired due to some logical conclusion as part of the business logic that is being implemented via JavaScript...the event is not as a result of a user's input on a form.

To be more specific, this is part of an Online Testing environment. The user takes a test online, and submits it. JavaScript is used to "grade" the test. If the user passes the test, I wish to update the database that the test has been passed. The update is initiated via the grading process done in JavaScript...not a user's mouse click of any kind.

Any words of insight will be most appreciated.

Thanks for your attention!

Jim

Quote Reply
Re: Calling a custom dbman routine from JavaScript In reply to
It's not directly possible to call a dbman routine (custom or otherwise) from JavaScript because your JS code runs on the client, while db.cgi runs on the server.

You could, however, generate an URL to view records in your JavaScript and load that page. Alternativly, the URL could be opened in another frame or window. I don't remember the exact syntax. The created URL could be used to add records, query the db, etc.

Provide more details, and I'll make some suggestions.


Quote Reply
Re: Calling a custom dbman routine from JavaScript In reply to
Eric:

Thanks for responding.

Yes, it's true that dbman is designed to respond to explicit user input either via forms (POST) or by anchors (<a href="$db_script_url?..."). As I indicated, I wish to do the same thing but to initiate the call in JavaScript without the direct click of a user's mouse (but as a reult of a logical step in the process flow, in this case that an online test has been successfully passed).

There is a way to do this, and that is to emulate a user's mouse click in JavaScript via the click() method. This means that the following in JavaScript is possible:

===================
.
.
. // grade the online test
[..grade test S1...] // S1 is a code for a particular test
.
.
.
if (testS1Passed)
setFlagTestS1Passed = true
.
.
.
// anchor that points to update database routine:
// (userID is known in a javascript variable)
row = '<a name="updateDBwithS1" href="/perl/dbman/db.cgi?db=certificates&add_Test_Passed=1&CourseCode=S1&UserID=' + userID + '"></a>'
document.write(row)
.
.
.
if (setFlagTestS1Passed)
updateDBwithS1.click()
.
.
.
===================

In Perl the routine is just a simple database add record function:

# --------------------------------------------------------
sub add_Test_Passed {
# --------------------------------------------------------
# db fields: @db_cols = qw(UserID Date ID CourseCode);
# UserID and CourseCode come via GET and are in the %in{} hash

&switch_to_tests_passed;

$in{'Date'} = &get_date;

open (ID, "<$db_id_file_name") or
&cgierr("error in get_defaults. unable to open id file: $db_id_file_name.\nReason: $!");
if ($db_use_flock) { flock(ID, 1); }
$in{$db_key} = <ID> + 1;
close ID;

open (DB, ">>$db_file_name");
print DB &join_encode(%in);
close DB;

open (ID, ">$db_id_file_name");
print ID $in{$db_key};
close ID;
}

1

===================

The problem with this (I believe) is that I am getting a faulty return code since the screen shows the infamous "Internal Server Error" message. However, the database does get written to as it should, and if I augment the Perl code with

print "Content-type: text/html\n\n";
print "UserID=$in{'UserID'}
\n";
print "CourseCode=$in{'CourseCode'}
\n";
print "Date=$in{'Date'}
\n";

the screen show that the values are being passed as they should.

This technique does work, JavaScript does emulate a user's click on the anchor and the routine updates the database...but fails at the point that I believe is a return code fault. Any thoughts?

Jim

Quote Reply
Re: Calling a custom dbman routine (try 2) In reply to
 
Ack! There are better ways than write()! For example:

Code:
function clicked() {
window.location = "/perl/dbman/db.cgi?db=certificates&add_Test_Passed=1&CourseCode=S1&UserID=" + userID;
return 0;
}

...

<a onclick="clicked()">
or

Code:
function clicked() {
document.my_form.href = "/perl/dbman/db.cgi?db=certificates&add_Test_Passed=1&CourseCode=S1&UserID=" + userID;
document.my_form.submit();
}

...

<form name="my_form">
...
<input type="button" onclick="clicked()">
...
</form>