Developer Forums | About Us | Site Map


Useful Lists

Web Host
site hosted by netplex

Online Manuals

Using HTML forms with PHP
By Nicholas Chase - 2003-12-12 Page:  1 2 3 4 5 6

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

           <td valign="top">Crew species:  </td>
               <select name="crew[]" multiple="multiple">
                   <option value="xebrax">Xebrax</option>

                   <option value="snertal">Snertal</option>
                   <option value="gosny">Gosny</option>

           <td colspan="2" align="center">
              <input type="image" src="button.gif" name="formbutton"/>


Figure 2. Graphic button on form
Adding a graphic button

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:


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.


In this article, you looked at some of the ways to access information submitted by a user through an HTML or XHTML form. How you handle this information will depend on the version of PHP you're using and whether you can access your form variables as globals. In any case, form values are available as arrays, and you can use the properties of arrays to loop through any available values.

View Using HTML forms with PHP Discussion

Page:  1 2 3 4 5 6 Next Page: Resources

First published by IBM developerWorks

Copyright 2004-2018 All rights reserved.
Article copyright and all rights retained by the author.