PHP and HTML
PHP and HTML are interoperable: PHP can generate HTML, and HTML can pass information to PHP. 1. How do I encode/decode when I pass values with a form/URL? 2. I am tagging with <input type="image">, but the $ and $ variables are not available. Where are they? 3. How do I create an HTML <form> array? 4. How do I get all the result values from a multi-check box?
1. How do I encode/decode when I pass a value using a form/URL?
In several stages, encoding is very important. Suppose you have a string variable $data, which contains some characters you want to pass in an unencoded manner, and you need to code through the following stages:
HTML explanation. In order to represent any string, you must include it in double quotes, and the HTML special characters must be written using htmlspecialchars.
URL: The URL consists of several parts. If you want your data to be regarded as part of the URL, you must encode it with urlencode().
Example 52-1. Hidden HTML form elements <?php echo "<input type=hidden value=\"" . htmlspecialchars($data) . "\">\n";?>
Note: You cannot use urlencode() $data here, because urlencode() data is the responsibility of the browser. Most browsers can handle such data correctly. Regardless of the method (GET POST). You can only consider GET requests, because POST requests are usually hidden.
Example 52-2. User edits data <?php echo "<textarea name=mydata>\n"; echo htmlspecialchars($data)."\n"; echo "</textarea>";?>
Note: The data displayed in the browser window will be decoded to readable because the browser will interpret the HTML tag.
Once submitted, whether GET or POST, the browser will URL encode the data when transferring the data, and PHP will decode it. Everything is done automatically and you don't need to do anything.
Example 52-3. In the URL string <?php echo "<a href=\"" . htmlspecialchars("/?stage=23&data=" . urlencode($data)) . "\">\n";?>
Note: In fact, you are writing a GET request yourself, so encoding it with urlencode() is indispensable.
Note: You need htmlspecialchars() the entire URL string because the URL string is part of the HTML-property. In this case, the browser first reverses the value of -htmlspecialchars() and then sends out the URL. PHP will recognize this URL string because you encoded it with urlencoded().
You will find that & is replaced with & in the URL string. Although most browsers do not encode it will help you, not all of them will do it. So even if you are writing a static URL, you need to use htmlspecialchars() to encode the URL.
2. I'm tagging with <input type="image"> but the $ and $ variables are not available. Where are they?
In submitting a form, you may use an image control instead of using the standard submit button: <input type="image" src="" name="foo">When When the user clicks somewhere on the image control, the form is submitted to the server and has two additional variables: and .
Because $ and $ are invalid variable names in PHP, they are automatically converted to $foo_x and $foo_y. That is, the dots are replaced with underscores.
3. How do I create an HTML <form> array?
To send your form results as an array to a PHP script, you can name the <input>, <select> or <textarea> elements like this: <input name="MyArray[]"><input name="MyArray[]"><input name="MyArray[]"><input name="MyArray[]">Note that the brackets after the element name cannot be omitted, that is, it makes the result an array. You can arrange them into different arrays by element names: <input name="MyArray[]"><input name="MyArray[]"><input name="MyOtherArray[]"><input name="MyOtherArray[]">The above code produces two arrays, MyArray and MyOtherArray, sent to PHP. Of course, you can also specify the key value for your array: <input name="AnotherArray[]"><input name="AnotherArray[]"><input name="AnotherArray[email]"><input name="AnotherArray[phone]">AnotherArray The array will hold subscripts 0, 1, email and phone.
Explanation: It is optional to indicate the subscript value in the element name. If you do not indicate it, the array will be filled in order in which the elements appear in the form. For example, in our first example, the subscripts of the array are: 0, 1, 2 and 3.
See Array function and PHP external variables.
4. How do I get all the result values from a multi-check box?
Multiple selection boxes in HTML are used to let users select multiple values from the list. These values are then sent to the form processing script. The problem is that they all have the same variable name. For example: <select name="var" multiple>Each selected item will be passed to the processing script like this: var=option1var=option2var=option3 Each value overwrites the value of the previous item. The $var solution is to use the "form element array" feature of PHP. Here is: <select name="var[]" multiple>PHP treats $var as an array. Each selected item is assigned to an array unit. The first item is $var[0], the next item is $var[1], and so on. The count() function can be used to determine how many options there are in the array. If necessary, the sort() function can also be used to sort the array.
If you are using JavaScript, it may be an error using the element name reference directly. You should use its numeric index, or put the variable name in single quotes. For example: variable = [0].elements['var[]'];