Home  >  Article  >  Backend Development  >  PHP and HTML_PHP Tutorial

PHP and HTML_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 16:10:031013browse


PHP and HTML
PHP and HTML can interoperate: PHP can generate HTML, and HTML can pass information to PHP. 1. When I use a form/URL to pass a value, how do I encode/decode it? 2. I am using tag, but the $foo.x and $foo.y variables are not available. Where are they? 3. How do I create an HTML

array? 4. How do I get a multiple from a Select the box to get all the result values?
1. When I use a form/URL to pass the value, how do I encode/decode it?
Encoding is very important in several stages. Suppose you have a character The string variable $data, which contains some characters that you want to pass unencoded, undergoes the following stages of encoding:

HTML explanation. In order to represent any string, you must enclose it in double quotes , and if it is an HTML special character, use the htmlspecialchars writing method.
URL: A URL is made up of several parts. If you want your data to be seen as part of the URL, you must encode it with urlencode().


Example 52-1. Hidden HTML form elements n";?>
Note: You cannot use urlencode() $data here, because urlencode() data is the responsibility of the browser. Most browsers can handle such data correctly. No matter what method is used (GET POST). You can just Consider GET requests, since POST requests are usually hidden.
Example 52-2. User editing data n"; echo htmlspecialchars($data)."n"; echo "";?>
Note: The data displayed in the browser window will be decoded to be readable because the browser interprets the HTML markup.
Once submitted, whether GET or POST, the browser will perform the URL when transmitting the data Encode it and PHP will decode it. Everything happens automatically and you don't need to do anything.
Example 52-3. In URL string n";?>
Note: You are actually writing a GET request yourself, so encoding it with urlencode() is indispensable.
Note: You need to htmlspecialchars() the entire URL string, because the URL string is part of the HTML-attribute. In this case, the browser first de-htmlspecialchars() the value, and then sends the URL. PHP will recognize this URL string , because you encoded it with urlencoded().
You will find that & is replaced by & in the URL string. Although you do not encode most browsers will do it for you, but not all of them will Yes. So even if you are writing a static URL, you need to use htmlspecialchars() to encode the URL.

2. I am using the tag, but $ The foo.x and $foo.y variables are not available. Where are they?
In submitting a form, it is possible to use an image control instead of using the standard submit button: When the user clicks somewhere in the image control, the form is submitted to the server with 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 by Underlined.
3. How do I create an HTML array?
To have your form results sent to a PHP script as an array, you can give , Note the element The square brackets after the name cannot be omitted, that is, it makes the result an array. You can arrange them into different arrays by the element names: the above The code generates two arrays, MyArray and MyOtherArray, and sends them to PHP. Of course, you can also specify key values ​​for your arrays: AnotherArray array will hold subscripts 0, 1, email and phone.

Note: It is optional to indicate the subscript value in the element name. If you do not indicate it, the array will be in the order in which the elements appear in the form. to fill in sequence. 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 from a multiple Checkbox to get all result values?
Checkboxes in HTML are used to allow the user to select multiple values ​​from a list. These values ​​are then sent to the form processing script. The problem is that they all have the same variable name. For example: PHP will treat $var as an array. Each selected item will be 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, use it directly Element name quoting can go wrong. You should use its numeric index, or put variable names within single quotes. For example: variable = documents.forms[0].elements['var[]'];

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/314345.htmlTechArticlePHP and HTML PHP and HTML can interoperate: PHP can generate HTML, and HTML can pass information to PHP. 1 . When I use a form/URL to pass a value, how do I encode/decode it? 2. I am using inp...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:example2.php_PHP tutorialNext article:example2.php_PHP tutorial