Friday, April 06, 2007

The imagebutton

This week i needed a table with buttons to manipulate the individual rows. My first instinct was to use button tags so i could use their value instead of a hidden input but that was without thinking about internet explorer so i used the imagebutton.

The problem in internet explorer is that the value is always the text of the button. the image button is just an input tag with image as type. To make the button unique just add a number after the name.

for($i=0;$i<10;$i++){
?><p><input type="image" name="button<?php echo $i; ?>" src="image.jpg" value="button"></p><?php
}

Now there are ten unique buttons. The following step is to get the button from the post array. In the example we have a limited, static number of buttons but if it's a databasetable you need to connect the buttons with and unique field in the database so there can't occur any mistakes.
Because $_POST['button0'] will only return the button text in internet explorer so you need to use the mouse click coordinates of the button that get send. The coordinates are button0_x and button0_y.

for($i=0;$i<10;$i++){
if(strlen(array_search('button'.$i.'_x',array_keys($_POST))) > 0){
// do something
}
}

Now that you know click coordinates are send you can use them to make one button act as two.

$click = $_POST['test_x'];
if($click > 24){
$test = 'Delete button';
}else{
$test = 'Update button';
}

The previous code is the code i used on the example page.

conclusion


The image button can spare you an hidden input and act as two buttons.

No comments: