Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: CGI question: elegant way to have three form image buttons doing different things?

by Chady (Priest)
on Sep 13, 2001 at 10:25 UTC ( [id://112119]=note: print w/replies, xml ) Need Help??


in reply to CGI question: elegant way to have three form image buttons doing different things?

Have your image submits with the same name and place a value:

<input type="image" name="action" value="invite" alt="Invite" src="inv +ite.gif"> <br> <input type="image" name="action" value="view" alt="View Results" src= +"view.gif"> <br> <input type="image" name="action" value="leave" alt="Leave" src="exit. +gif">

then you test that value in your script:

my $action = $cgi->param('action'); if ($action eq 'invite') { # invite code } elsif ($action eq 'view') { # view ... ..

He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

Chady | http://chady.net/

Replies are listed 'Best First'.
Re: Re: CGI question: elegant way to have three form image buttons doing different things?
by t'mo (Pilgrim) on Sep 13, 2001 at 17:27 UTC

    Nice. Giving the images the same name, and associating a value is probably the most "elegant" way.

    As an alternative to a series of if...elsif...else statements, you could also use a hash of acceptable action names, paired with a sub, e.g.:

    my %actions = ( invite => sub { ... }, view => sub { ... }, leave => sub { ... }, ); my $action = $cgi->param('action'); $actions{$action}->() if exists $actions{$action};

    Update:

    ...except that from an HTML perspective, I don't think that using the same name for the input/image elements will work. According to the the HTML 4 standard,

    When a pointing device is used to click on the image, the form is submitted and the click coordinates passed to the server. The x value is measured in pixels from the left of the image, and the y value in pixels from the top of the image. The submitted data includes name.x=x-value and name.y=y-value where "name" is the value of the name attribute, and x-value and y-value are the x and y coordinate values, respectively.

    So, it looks like in the CGI script, you'd have to get the coordinates; you can't assign a value to that input/image element, since it acts like a submit button. So, we're probably again reduced to haveing to use separate names, and detecting the different names, or alternatively, knowing the coordinates that will be returned and detecting those.

    Or, like I mentioned before (and like wildooUK mentions below), use JavaScript. :-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://112119]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2024-03-28 14:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found