Rainmaker has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I'm working on a project that requires calling a perl function from the HTML code.
After searching online i found a tool called "Sajax" which can actually do this. http://www.modernmethod.com/sajax/
I made a simple application using this tool and it worked. I'm trying to use this tool in my form but its giving me weird errors.Presently i'm putting this tool to just print a string.
Actually i'm calling the javascript function add() on the onChange()event of a drop downbox which calls the perl wrapper function x_populate() to print a string.
I get the following error:-
Error: uncaught exception: [Exception... "Not enough arguments nsIDOMHTMLSelectElement.add" nsresult: "0x80570001 (NS_ERROR_XPC_NOT_ENOUGH_ARGS)" location: "JS frame :: unknown filename> :: onchange :: line 0" data: no]
The strange part is if i call the add() function on the onClick() event of a button it actually works. I dont know whats the issue with the onChange() event.
I guess this is a javascript error but how to resolve it.
Sample Code:
function add(F){ x_populate(print1); } function print1(N1) { document.write(N1); document.write('Its Me'); } my $msub = sub { return 'Hey'; }; Sajax::rs_init(); Sajax::rs_register("populate",$msub); //followed by some other Sajax functions //calling add() function <SELECT NAME="selectCourseDept" onChange="add(); return false;">

Replies are listed 'Best First'.
Re: Calling Perl function from HTML code - using SAJAX
by ikegami (Patriarch) on Jul 29, 2005 at 19:54 UTC

    Problem #1: SELECT objects have an add method, so it's getting called instead of your add function.

    Problem #2: You call add with no arguments, yet you specified it requires ones.

    Here's the fix, and I cleaned it up to adhere to convention.

    Perl ---- use Sajax; sub populate { return 'Hey'; } Sajax::rs_init(); Sajax::rs_register("populate", \&populate); ... JS outputed by Perl ------------------- function do_populate() { x_populate(do_populate_cb); } function do_populate_cb(N1) { document.write(N1); document.write('I\'ts Me'); } HTML outputed by Perl --------------------- ... <SELECT NAME="selectCourseDept" onChange="do_populate(); return false; +"> ...

    Passing the selected course around:

    Perl ---- use Sajax; sub populate { my ($course) = @_; return "You selected $course"; } Sajax::rs_init(); Sajax::rs_register("populate", \&populate); ... JS outputed by Perl ------------------- function do_populate() { x_populate(selectCourseDept.value, do_populate_cb); } function do_populate_cb(feedback) { feedback_field.innerText = feedback; } HTML outputed by Perl --------------------- ... <p> <SELECT NAME="selectCourseDept" onChange="do_populate(); return false; +"> ... </SELECT> </p> <p>You selected: <span id="feedback_field"></span></p> ...

    Untested.

      Thanks a ton ikegami. Yeah the code ran without any problems. I'll get back to you if i face any other problem. Thanks a lot again. Gaurav.
Re: Calling Perl function from HTML code - using SAJAX
by esskar (Deacon) on Jul 29, 2005 at 19:51 UTC
Re: Calling Perl function from HTML code - using SAJAX
by Joost (Canon) on Jul 29, 2005 at 19:56 UTC