Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Handling multiple image submit buttons in PHP 1

Status
Not open for further replies.

Dweezel

Technical User
Feb 12, 2004
428
GB
I have the following within a html form:
Code:
<input type='image' name='stamp' value='1134' src='../images/back.gif'>
<input type='image' name='stamp' value='2465' src='../images/forward.gif'>

The values are generated in a php script. I need to get the value of whichever button was pressed.

How would I go about accessing these values in the php page that I post the form to?
 
When you click on an image, what is returned to the script is the x- and y-coordinates of where on the image you actually clicked. In the case of your form (assuming a POST-method input), $_POST will contain two elements: "stamp_x" and "stamp_y".

Since you're using the same form element name for both images, there is no way to tell inside your script on which image the user clicked. I recommend that you name them uniquely.

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Thanks Sleipnir. I've just tried your method but I'm still having problems. I've changed my form code as you can see below.

Code:
<input type='image' name='back' value='1134118800' src='../images/back.gif'>
<input type='image' name='forward' value='1134140400' src='../images/forward.gif'>

I set up the following echo in the page that the form is sent to (It's actually sent back to itself):

Code:
echo $_POST['back_x'];

Unfortunatley rather then echoing 1134118800 it echos 40. Is this one of the co-ordinates? If it is, how do I access the value?
 
AS I have said before, $_POST['back_x'] will be the x-coordinate of where on the image you clicked (coordinates are from the upper-left corner). A browser will not transmit a "value" attribute to the server, only the <fieldname>-x and <fieldname>_y values. This is IAW




Want the best answers? Ask the best questions!

TANSTAAFL!!
 
OK. Thanks for clearing that up. I was trying to avoid using javascript but it looks like that's not going to be possible.

Thanks again.
 
Why are you going to use JavaScript?

Your scripts don't necessarily have to pass values around. Your scripts just need to know which image was clicked. Your scripts should be able to reconstruct the values.

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
But here's a trick that might be of help:

I have created an HTML page of the form:

Code:
<html><body><form method="post" action="show_post.php">
<input type="image" name="stamp[1134118800]" src="redstar.gif" value="red">
<input type="image" name="stamp[1134140400]" src="greenstar.gif" value="green">
</form></body></html>

The script to which the form submits, show_post.php, reads:

Code:
<?php
print '<html><body><pre>';

print_r ($_POST);

print '</pre></body></html>';
?>

When I click on the first image, I get:

[tt]Array
(
[stamp] => Array
(
[1134118800] => 21
)

)[/tt]

When I click on the second image, I get:

[tt]Array
(
[stamp] => Array
(
[1134140400] => 15
)

)[/tt]

This works with Opera, IE, and Mozilla.

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
That's excellent Sleipnir, works like a charm. I'd give you another star if I could :)

Thanks a lot for your time.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top