1. Home
  2. Computing & Technology
  3. Perl

Working with CGI.pm In-Depth

Creating an Image Based Submit Button

From About.com

Tired of the old, standard submit buttons? Here's a quick CGI.pm way to get an image based submit button for your forms. Not only will this submit the form when the image is clicked, but it will return the x/y coordinated of the click as a parameter set along with the rest of the form fields, so you could even do different things based on where the user clicks the image.

Let's look at a sample image_button:

print $cgi->image_button(
-name => 'my_button',
-src => 'images/button.gif'
);
This will produce the following HTML:
<input type="image" name="my_button" src="images/button.gif" />
The only one we haven't seen already in the general form attributes section is the -src attribute. This should point to the image file itself, relative to your HTML output. In this case, it's calling a file in the images/ directory called button.gif.

Once you submit the form, you'll have access to two parameters that represent the x/y coordinates clicked on when the image was used to submit the form. They will take their names from the -name parameter and are available in the parameters as param('my_button.x') and param('my_button.y').

Explore Perl

More from About.com

  1. Home
  2. Computing & Technology
  3. Perl
  4. CGI & Web
  5. Working with CGI.pm In-Depth - How to create buttons from images

©2008 About.com, a part of The New York Times Company.

All rights reserved.