PHP and HTML
PHP and HTML can interoperate: PHP can generate HTML, while HTML can transmit information to PHP. 1. How can I encode/decode the code when I use a form/URL to transmit values? 2. I am marking with <input type = "image">, but the $ foo. x and $ foo. y variables are unavailable. Where are they? 3. How do I create an HTML <form> array? 4. How can I obtain all the result values from a multiple-choice box?
1. How do I encode/decode a value using a form/URL?
In several stages, encoding is very important. assume that you have a string variable $ data, which contains some characters that you want to pass in unencoded mode. The encoding goes through the following stages:
HTML interpretation. to represent any string, you must include it in double quotation marks and use htmlspecialchars for special characters in HTML.
URL: a URL consists of several parts. If you want your data to be viewed as part of a URL, you must encode it with urlencode.
Example 52-1. Hidden HTML form elements <? Php echo "<input type = hidden value = \" ". htmlspecialchars ($ data)." \ "> \ n";?>
Note: urlencode () $ data cannot be used here, because urlencode () data is the responsibility of the browser. most browsers can process such data correctly. regardless of the method used (get post ). you can only consider GET requests, because POST requests are usually hidden.
Example 52-2. user editing data <? Php echo "<textarea name = mydata> \ n"; echo htmlspecialchars ($ data). "\ n"; echo "</textarea>" ;?>
Note: The data displayed in the browser window will be decoded into readable, because the browser will explain the HTML Tag.
Once submitted, regardless of GET or POST, the browser will perform URL encoding during data transmission, and PHP will decode it. Everything is automatically performed and you do not need to do anything.
Example 52-3. In the URL string <? Php echo "<a href = \" ". htmlspecialchars ("/nextpage. php? Stage = 23 & data = ". urlencode ($ data)." \ "> \ n";?>
Note: In fact, you write a GET request yourself, so using urlencode () to encode it is indispensable.
Note: You need to htmlspecialchars () The whole URL string, because the URL string is part of the HTML-attribute. in this case, the browser first returns the value-htmlspecialchars () and then sends the URL. PHP will recognize this URL string because you have encoded it with urlencoded.
You will find that & is replaced with & in the URL string &. although you do not code most browsers, not all of them can. so even if you are writing a static URL, you need to use htmlspecialchars () to encode the URL.
2. I am marking with <input type = "image">, but the $ foo. x and $ foo. y variables are unavailable. Where are they?
In submitting a form, an image control may be used instead of the standard submit button: <input type = "image" src = "image.gif" name = "foo"> When a user clicks a location of the image control, the form is submitted to the server, there are two additional variables: foo. x and foo. y.
Because $ foo. x and $ foo. y 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 arrays to PHP scripts, you can name the <input>, <select> or <textarea> element as follows: <input name = "MyArray []"> <input name = "MyArray []"> <input name = "MyArray []"> <input name = "MyArray []"> note that brackets after element names cannot be saved, it makes the result an array. you can arrange the element names in different Arrays: <input name = "MyArray []"> <input name = "MyArray []"> <input name = "mytherarray []"> <input name = "mytherarray []"> the code above generates two arrays, myArray and mytherarray, sent to PHP. you can also specify the key value for your array: <input name = "AnotherArray []"> <input name = "AnotherArray []"> <input name = "AnotherArray [email]"> <input name = "AnotherArray [phone] "> the AnotherArray will hold the subscript 0, 1. email and phone.
Note: It is optional to indicate the tag value in the element name. if it is not specified, the array will be filled in order of the elements in the form. for example, in the first example, the subscript of the array is: 0, 1, 2 and 3.
See Array functions and PHP external variables.
4. How can I obtain all the result values from a multiple-choice box?
The multiple selection box in HTML is used to allow users to select multiple values from the list. these values are then sent to the form's processing script. the problem is that they all have the same variable name. for example, <select name = "var" multiple> each selected item will pass the value to the processing script as follows: var = option1var = option2var = option3. Each value overwrites the value of the previous item. $ var the solution is to use the "form element array" feature of PHP. <select name = "var []" multiple> PHP regards $ 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 the number of selection items in the array. If necessary, you can also use the sort () function to sort the array.
If you are using JavaScript, using element name reference may cause errors. you should use its digital index, or put the variable name in single quotes. for example: variable = documents. forms [0]. elements ['var [] '];