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

Hi Monks,

It is not a perl question exactly, but I think it is related anyway. Could you please explain why the first input (button1) does not generate parameter value? Here is the code:

#!/usr/bin/perl -w use strict; use CGI qw /:standard/; my $q = new CGI; print header, start_html; print ("Submitted:", $q->param(), hr) if $q->param(); print <<EOF; <form method="post"> <input type="button" name="button1" value="button1" onclick="this.for +m.submit()" /> <input type="submit" name="button2" value="button2" /> </form> EOF print $q->end_html; exit;
When I click on "button1", I can't see anything except the form itself, and "button2" works as expected. What am I missing?

--dda

Replies are listed 'Best First'.
•Re: <input type="button"> does not generate parameter=>value pair
by merlyn (Sage) on Jul 15, 2003 at 16:49 UTC
    Don't use JavaScript, especially for actions that already have established non-JavaScript semantics.

    I boggle when I see JS abused in such a fashion. JS is filtered by security web filters, and disabled by more people today than ever. Your site should still work fine without JS: it should use JS only to enhance the experience.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

Re: <input type="button"> does not generate parameter=>value pair
by Skeeve (Parson) on Jul 15, 2003 at 08:49 UTC
    Maybe because this.form.submit() doesn't return true and if it would it would be too late to submit button1's value.
      Well, I tried both onclick="this.form.submit();return true;" and onclick="return this.form.submit();" - nothing works... The only thing came to my mind - to create another hidden varuable and to assign a value to in in "onclick" handler.

      --dda

        You didn't get what I mean!
        You use a submit button. If you supply an onclick handler, this one has to return true in order for the button to submit anything.
        But your onclick handler already does the submit.

        Question: What do you want to achieve?

(jeffa) Re: <input type="button"> does not generate parameter=>value pair
by jeffa (Bishop) on Jul 15, 2003 at 15:22 UTC
    Why use JavaScript? This works perfectly well for me:
    #!/usr/bin/perl -T use strict; use warnings; use CGI qw(:standard); print header, start_html; print ("Submitted:", param(), hr) if param(); print start_form, submit('button1'), submit('button2'), end_form, end_html ;
    Note a couple style issues: don't mix 'hand coded' HTML with CGI.pm HTML generating methods and don't mix the OO style with the procedural style.

    If this still doesn't do what you want, you will need to elaborate a bit more (and, yes i read Re: Re: Re: Re: <input type="button"> does not generate parameter=>value pair and i still don't know what you really want).

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      Thanks jeffa. Suppose I add a text field to the form:
      #!/usr/bin/perl -T use strict; use warnings; use CGI qw(:standard); print header, start_html; print ("Submitted:", param(), hr) if param(); print start_form, textfield('text1'), br, submit('button1'), submit('button2'), end_form, end_html ;
      Now I need to enter some value into it and press 'Enter'. That way I will get 'button1' in param() - and I need 'button2' instead. I suppose it is due the fact that button1 has tabindex lower than button2... Is there a way to specify tabindex?

      By the way, when I pressed 'reply' I saw a button at the top of the screen - looks like my question title was parsed as HTML.

      --dda

        Ooooooooo ... that's what you are getting at. I recently had a similar problem that i solved by providing another interface. In this case, it was a significant benefit to do so, as it allowed me to enter data quickly (type numbers - tab - type numbers - tab, etc.). If you don't care to code another interface you can still use a simple keyboard trick: when you have finished entering data in the last textbox (assuming there will be more than one), press Tab twice and then the Space Bar once (or Enter key once, but the Space Bar is closer). This should skip over the first button and activate the second.

        Of course, you probably can do this with JavaScript ... but 'JavaScript' ne 'Perl' ;)

        That's odd that your question title was actually parsed ... tye fixed that a while back (see More HTML escaping), but it is possible someone was pmdevilin' around with the source at the time you saw this "Unidentified Formatting Occurence".

        jeffa

        L-LL-L--L-LL-L--L-LL-L--
        -R--R-RR-R--R-RR-R--R-RR
        B--B--B--B--B--B--B--B--
        H---H---H---H---H---H---
        (the triplet paradiddle with high-hat)
        
Re: <input type="button"> does not generate parameter=>value pair
by Mago (Parson) on Jul 15, 2003 at 17:30 UTC
    #!/usr/bin/perl use warnings; use strict; use CGI qw( :standard ); our $query = new CGI; our $method = $query->param("op"); print $query->header; print $query->start_html(-title=>'Exemple'); unless ($method) { print $query->start_form(-method=>"POST"); print ' Name: '; print $query->textfield(-name=>'name'); print $query->hidden(-name=>'op', -default=>"01"); print $query->submit('submit','submit'); print $query->reset('reset'); print $query->endform; } else { my $name = $query->param("name"); print "Name: $name"; } print $query->end_html;