David Scott wrote:
RA,

Please summarize your function so we can all benefit.
David,

With pleasure. The validation function is part of a much larger clinical information system, and validates the UK National Health Service number, which is a 10-digit unique patient identifier with a check-sum as the 10th digit. The whole application is delivered using CGI::Application and Template via the CAP::AnyTemplate plugin, and I think some of the problems I had involved header conflict issues between CGI::Ajax & CGI::Application (and some stupidity!), so the code below might require modification if used outside this particular environment.

Due to all the kind assistance I received I was working on two parallel methods, one based on CGI::Ajax, and the other on jQuery. I use CAP::AutoRunmode so I don't have to declare my rm's in setup(), but it also works fine without.

1) CGI::Ajax method:
a) In the run-mode that sets up the web-page which contains the AJAX code:

sub my_runmode : Runmode {
 my $self = shift;
 my $cgi = $self->query();

 # here we register the script & run-mode that will handle the AJAX
request:
my $pjx = new CGI::Ajax( validate_nhsno => 'my_app.cgi?rm=validate_nhsno' ); # could also use an Apache handler

 # this is CAP::AnyTemplate shorthand syntax (amend as necessary):
 return $self->template->fill('my_template', { ajax_js =>
$pjx->show_javascript() });
}

b) The validation routine - accepts the AJAX request:
sub validate_nhsno : Runmode {
 my ($self, $nhs_no) = @_;

 # do validation on $nhs_no & return result (ie valid/invalid)
}

c) Runmode that handles 'normal' web-form input via Submit button:
sub process_form_input : Runmode {
   # this is the usual CGI::App rm to process the web-form input
}

d) In the template:
[% ajax_js %]

<p>NHSNo: <input type="text" name="nhs_no" id="nhs_no"
   onBlur="validate_nhsno(['nhs_no'], ['bad_nhsno']); return true;" />
    <span id="bad_nhsno"></span></p>

The name of the function after the javascript onBlur (or onChange) event
handler should match the function originally registered to CGI::Ajax. Of
course you would define an appropriate style for #bad_nhsno (eg bold,
red, italic, etc). If you want to see the AJAX request output, set $pjx->JSDEBUG(1) before returning the template, and place this somewhere in your template: <div id='pjxdebugrequest'>

2) jQuery method (requires jquery library from jquery.com):

a)In the run-mode that sets up the web-page which contains the AJAX code:
Replicate my_runmode() above, but omit the AJAX stuff, just return the
template:
 return $self->template->fill(); # assuming template name same as runmode

b) The validation routine - exactly as above.
c) Runmode that handles web-form input - exactly as above.

d) In the template:
In the <head> section:
<script type="text/javascript" src="/jquery.js"></script>

In the <body> section:
<script type="text/javascript"><!--
 function validate_nhsno(myInput) {
   $('#bad_nhsno').load(
     'my_app.cgi', { # script containing validation function
       rm : 'validate_nhsno',
       "nhs_no" : myInput,
     }
   );
 }
--></script>

<p>NHS No: <input type="text" name="nhs_no" id="nhs_no"
 onBlur="validate_nhsno(this.value); return true;" />
 <span id="bad_nhsno"></span></p>


That's it! Both methods work for me but as I said above, there might be
configuration-specific code in there. Certainly if you use another
template or no template system you will need modifications, but I think
it should be adaptable to most environments (also works as Apache
handler script under mod_perl). See earlier messages for relative merits of CGI::Ajax & jQuery. I haven't tried prototype.js (yet), but no doubt it would work equally well.

Hope this helps. Anyone else feel free to contribute amendments,
rebuttals, clarifications, etc.


***************************************************************************
This e-mail is confidential and privileged. If you are not the intended
recipient please accept our apologies; please do not disclose, copy or
distribute information in this e-mail or take any action in reliance on its
contents: to do so is strictly prohibited and may be unlawful. Please
inform us that this message has gone astray before deleting it. Thank you
for your co-operation.
***************************************************************************


---------------------------------------------------------------------
Web Archive:  http://www.mail-archive.com/cgiapp@lists.erlbaum.net/
             http://marc.theaimsgroup.com/?l=cgiapp&r=1&w=2
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to