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

Hello. I making a buddy list feature for my website. Its a online teen community and I'd thought it would be neat to have a buddylist feature.

I have a text box, select multiple list, and 2 images. The images are submit buttons. (input type="image") One image says "add buddy" and the other one says "delete buddy." The add buddy would add that buddy typed in the text box and delete buddy would delete the buddy/ies in the list.

The form uses the POST method and action calls to buddylist.cgi.

Now how would I distinguish wether the user clicked the add buddy image to get to buddylist.cgi or delete buddy to get to buddylist.cgi?

<select multiple size="15" style="background-color: #FFC666; color: #0 +066CC; font-family: verdana,geneva,courier; font-size: 12;" name="bud +dies"> EOF open(IsF, "<$buddyfile"); @buddylist = <IsF>; close(IsF); foreach $buddylist (@buddylist) { chomp $buddylist; print "<option value=\"$buddylist\">$buddylist</option>"; } print <<EOF; </select><br> <input type="text" value="" name="buddyname"><br>
<input type="image" src="http://www.teen-reality.com/images/manav/bl/a +ddbuddy.jpg" name="bloption" value="add"> <input type="image" src="http://www.teen-reality.com/images/manav/bl/d +eletebud.jpg" name="bloption" value="delete">
</form> EOF
---------

Thats the form printed out from the CGI.
I tried this for the PERL code to distinguish wether the user wants to add or delete a buddy.

$bloption = $INPUT{'bloption'); if ($bloption eq "add") {etc. } else { delete buddy}

Replies are listed 'Best First'.
Re: two submit buttons in a form
by sutch (Curate) on Mar 12, 2001 at 07:59 UTC
    Test the value of the parameter named "bloption". For example, if your CGI object is $q, try:

    if ($q->param("bloption") eq "add") { # add buddy code here } elsif ($q->param("bloption") eq "delete") { # delete buddy code here }
      I'm not sure if this is expected behavior, or a cavaet, or something else... when I do this (two submits on the same page) using the same NAME parameter, I get back from CGI with that name parameter an array, which only has one elements, that being the VALUE of the submit button that was hit. It's easy enough to check to see if what CGI returns is an array or not, but it seems odd and undocumented. The only thing that might be affecting this is that I use short phrases for the VALUEs, so the multiplicity of words might be causing this, but seems doubtful.
      Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
        The only way that I can duplicate this behavior is to request the value(s) of the parameter in list context:

        my @value = $q->param("bloption");

        Otherwise, CGI.pm returns a scalar with the value of bloption. Examining the URL of the submitted form (that contains the two submit buttons), I can see that only one value for bloption is actually being submitted:

        http://localhost/~sutch/test/index.cgi?bloption=add
Re: two submit buttons in a form
by Chady (Priest) on Mar 12, 2001 at 12:28 UTC

    or maybe, you can use a radio button (like the ones for the voting system here) and one submit.

    <input type="text" value="" name="buddyname"><br> <input type="radio" name="action" value="add"> Add Buddy <br> <input type="radio" name="action" value="remove"> Add Buddy <br>
    and later on...
    my $action = $q->param('action'); # this is assuming you're using CGI. +pm; if ($action eq 'add') { &addBuddy; else { &deleteBuddy; }

    As far as I can see, this is way better than two submits.


    Chady | http://chady.net/

      But this is much worse from a user interface perspective, as it requires one to two clicks, as opposed to just one. It also introduces more of a chance of error, since the same button does adding and deleting. (Not to mention that both your buttons are labelled "Add User" *grin*)