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

I have a JavaScript function that disables the submit button on a form to prevent duplicate submissions. It works fine.

The problem I'm running into is that when it submits the form, I'm losing my param that's on the submit button.

Here's where I declare my form.

$data .= $cgi->start_form(-method=>'POST', + -action=>$cgi->url(-relative=>1), -onsubmit=>"return disableForm(this);");

And here are my buttons.

$data .= $cgi->submit(-name=>'commit_part', -value=>'Save'); $data .= $cgi->submit(-name=>'cancel_edit_part', -value=>'Cancel'); $data .= $cgi->end_form();

Looking at my logs, it's clear that when I use the -onsubmit=>"return disableForm(this);" I don't get the 'commit_part' that I need when I do  elsif ($cgi->param('commit_part')) later on.

How can I call the script and get the param? Thanks.

Replies are listed 'Best First'.
Re: JavaScript and form submission
by Corion (Patriarch) on Nov 21, 2006 at 14:41 UTC

    I never found the values of submit buttons to be of much use as CGI parameters, because IE and FireFox have different ideas on what to send there - one sends the caption of the button and the other sends the value.

    Why don't you just add a hidden parameter that tells you that the page was not requested in its initial state but submitted as a form?

      Then how do I tell the difference between the Submit and Cancel buttons?

        If you can use Javascript, just set the value of the hidden field accordingly. If you're supporting non-Javascript too, make the "cancel" link a GET request instead of a button.

      I never found the values of submit buttons to be of much use as CGI parameters, because IE and FireFox have different ideas on what to send there - one sends the caption of the button and the other sends the value.

      This is a bug in IE, since the spec is fairly clear on the matter, but not relevent to this issue since the submit() method in CGI.pm generates an input of type "submit", and not a button with different display text and value.

Re: JavaScript and form submission
by themage (Friar) on Nov 21, 2006 at 15:10 UTC
    Hi rashley,

    In true, the Value and the Caption of a submit button are usually the same thing, and as far as I had found it works ok with every browser.

    Your problem, however, is a diferent thing. You want to disable the buttons, but you want the button value to be submitted anyway.

    The way I would do it is by having a hidden field and then assign it the value of the pressed button, or by changing the buttons from submit to normal buttons, with a javascript handler in the onrelease that change the hidden field value and submit the form.

Re: JavaScript and form submission
by rashley (Scribe) on Nov 21, 2006 at 15:28 UTC
    I think the cleanest solution for me is going to be setting the param in the JavaScript. Now I just have to figure out how to do that, but that's not a question for this venue ;)

    Thanks for the help, guys!

Re: JavaScript and form submission
by dorward (Curate) on Nov 22, 2006 at 13:24 UTC

    I'd approach the problem by not trying to prevent duplicate submissions, but to handle them in a smarter way.

    Include an identifier in the form (using a hidden input). If a submission comes in with an identifier you've seen before, then treat it as an update to the existing data rather than new data.

      No worries, with Corion's help I've got it working now.