PHP and HTML interact a lot: PHP can generate HTML, and HTML
can pass information to PHP. Before reading these faqs, it's
important you learn how to
retrieve variables from outside of PHP. The manual page on
this topic includes many examples as well. Pay close attention to
register_globals means to you too.
What encoding/decoding do I need when I pass a value through a form/URL?
There are several stages for which encoding is important. Assuming that
you have a string
Example 8.1. A hidden HTML form element
It is wrong to urlencode()
Example 8.2. Data to be edited by the user
The data is shown in the browser as intended, because the browser will interpret the HTML escaped symbols.
Upon submitting, either via GET or POST, the data will be urlencoded by the browser for transferring, and directly urldecoded by PHP. So in the end, you don't need to do any urlencoding/urldecoding yourself, everything is handled automagically.
Example 8.3. In a URL
In fact you are faking a HTML GET request, therefore it's necessary to manually urlencode() the data.
You need to htmlspecialchars() the whole URL, because the URL occurs as value of an HTML-attribute. In this case, the browser will first un-htmlspecialchars() the value, and then pass the URL on. PHP will understand the URL correctly, because you urlencoded() the data.
You'll notice that the
I'm trying to use an <input type="image"> tag, but
When submitting a form, it is possible to use an image instead of the standard submit button with a tag like:
When the user clicks somewhere on the image, the accompanying form
will be transmitted to the server with two additional variables:
Spaces in request variable names are converted to underscores.
How do I create arrays in a HTML <form>?
To get your <form> result sent as an array to your PHP script you name the <input>, <select> or <textarea> elements like this:
Notice the square brackets after the variable name, that's what makes it an array. You can group the elements into different arrays by assigning the same name to different elements:
This produces two arrays, MyArray and MyOtherArray, that gets sent to the PHP script. It's also possible to assign specific keys to your arrays:
The AnotherArray array will now contain the keys 0, 1, email and phone.
Specifying an arrays key is optional in HTML. If you do not specify the keys, the array gets filled in the order the elements appear in the form. Our first example will contain keys 0, 1, 2 and 3.
How do I get all the results from a select multiple HTML tag?
The select multiple tag in an HTML construct allows users to select multiple items from a list. These items are then passed to the action handler for the form. The problem is that they are all passed with the same widget name. I.e.
Each selected option will arrive at the action handler as:
Each option will overwrite the contents of the previous
This tells PHP to treat