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. edit 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 aboveCodeTwo arrays, myarray and mytherarray, are generated and 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 processing script. The problem is that they all have the same variable names. For example:
<Select name = "Var" multiple> each selected item is passed 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 PHP "form element array" feature. The following is:
<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, an error may occur when you directly reference the element name. You should use its numeric index or put the variable name in single quotes. For example:
Variable = documents. Forms [0]. elements ['var [] '];