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

Hi I'm trying to work out how to build an Ajax form that uses XMLHttpRequest when javascript is available for validation when it is present and perl CGI for validation when it isnt.

Im using the code

<input type="button" value="Submit" id="submit" onClick="return check_ +values();"/>

So how will a perl script get the values from the form if javascript is turned off.

If i use

<input type="submit" value="Submit" id="submit" onClick="return check_ +values();"/>

then how do i stop the onclick() being called when javascript is not active. I've considered writing it using javascript but it seemed really clumsy and didn't work anyway,

<input type="submit" value="Submit" id="submit"<script type="text/java +script">document.write('onClick="return check_values();"')</script>/>

Is this even a perl question? Sorry if its not. But I'd love it if someone pointed me in the direction of a useful module on Cpan.

Replies are listed 'Best First'.
Re: Building an ajax form that degrades gracefully
by dorward (Curate) on Jun 19, 2006 at 22:08 UTC
    <form action="serverside.cgi" onsubmit="return myAjax()">

    Along with a regular: <input type="submit">

    Have the JavaScript function myAjax return false should it execute successfully - this will cancel the regular form submission. Otherwise return true (or let it die) and the form submission will proceed as normal.

Re: Building an ajax form that degrades gracefully
by Corion (Patriarch) on Jun 19, 2006 at 21:22 UTC

    If you are hell-bent on providing Ajax-style functionality, I found the following approach much more convenient and better degrading:

    1. Develop the site as plain HTML+CGI
    2. Write JavaScript to manipulate the DOM of your templates and add the wanted hooks dynamically.

    That way your HTML stays relatively minimal and the added functionality gets pulled in when your JavaScript code adds the necessary attributes. You should avoid document.write and use the real DOM methods though ;)

Re: Building an ajax form that degrades gracefully
by Belgarion (Chaplain) on Jun 20, 2006 at 02:48 UTC

    Maybe I'm missing something, but if Javascript is not enabled the onclick will not run at all. In other words, disable Javascript and the form works as if the onclick was not there.

Re: Building an ajax form that degrades gracefully
by Anonymous Monk on Jun 20, 2006 at 07:09 UTC

    Belgarion's comment is absolutely correct. Simply place the onclick handler and it will do what it is meant to do; it will execute if JavaScript is enabled, and will be ignored if JavaScript is disabled/not available.

    Also, your comment regarding "XMLHttpRequest when javascript is available for validation when it is present and perl CGI for validation when it isnt" sets off alarms for me. When using JavaScript for validation, it is detrimental to your server's health that you ALWAYS validate on the server side (via Perl for example), regardless as to whether it has already been validated via JavaScript. Your server will find itself in trouble if you depend on JavaScript weeding out invalid input. It's unfortunate that this generally means that you need to modify two code bases when you change validation requirements: once in JavaScript and once in the server-side language of your choosing. It's more code to satisfy the lovely convenience of client-side validation.

      It's unfortunate that this generally means that you need to modify two code bases when you change validation requirements

      Ah - not so. CGI::Ex::Validate - its been in use in various forms for 4 or 5 years.

      my @a=qw(random brilliant braindead); print $a[rand(@a)];
Re: Building an ajax form that degrades gracefully
by Rhandom (Curate) on Jun 20, 2006 at 16:58 UTC
    You should use something similar to CGI::Ex::Validate which handles validation nicely on the perl side or the javascript side - but doesn't require the javascript side.

    On another note - AJAX form validation can be somewhat silly. Essentially you are hitting the server to check the information, to see if you can submit the form, which if submitted will hit the server, which will necessarily need to recheck the information.

    The form validation needs to do as much as possible without hitting the server - which is what CGI::Ex::Validate does.

    my @a=qw(random brilliant braindead); print $a[rand(@a)];