Using HTML Forms With PHP
By Nicholas Chase
2004-01-09
Reader Rating:

One Final Note: Dots
Now that you have a form action page that will adapt to whatever form values you throw at it, you need to take a moment to look at one situation that often catches PHP programmers by surprise.
In some cases, rather than using a submit button, a designer opts for a graphic button, as shown in Figure 2 and the code shown in Listing 10.
Listing 10. Adding an image button
...
<tr>
<td valign="top">Crew species: </td>
<td>
<select name="crew[]" multiple="multiple">
<option value="xebrax">Xebrax</option>
<option value="snertal">Snertal</option>
<option value="gosny">Gosny</option>
</select>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="image" src="button.gif" name="formbutton"/>
</td>
</tr>
</table>
...
|
Figure 2. Graphic button on form
Notice that although there's only one image, there are two buttons, or desired outcomes, in the graphic. As a developer, you can tell where the user clicked by examining the x and y coordinates that are returned with the values. In fact, submitting the form as is might create a URL and querystring ending in:
...snertal&crew%5B%5D=gosny&formbutton.x=37&formbutton.y=14
|
Notice the .x and .y appended to the name of the button. If you were to submit the page and look at the results, however, you'd see:
ship = Midnight Runner
tripdate = 12-15-2433
exploration = yes
crew = snertal
crew = gosny
formbutton_x = 37
formbutton_y = 14
|
Notice that the period (.) has been converted to an underscore (_). This may seem a little odd, but it's necessary because variable names in PHP can't have periods in them, so $formbutton.x would be an illegal variable name. In fact, any periods in form names -- not just those for image buttons -- are converted to underscores.
First published by
IBM developerWorks
If you found this article interesting, you may want to read these as well:
» Protecting your PHP and HTML Source Code
» Publishing Newsletters Using PHP & MySQL - 4
» Publishing Newsletters Using PHP & MySQL - 3
» Publishing Newsletter Using PHP & MySQL - 2
» Publishing Newsletters Using PHP & MySQL
» Unix Webserver Crontab Basics
|