PHP and HTML

Source: Internet
Author: User
Tags arrays variables php and php script sort variable urlencode
PHP and HTML
PHP and HTML can interoperate: PHP can generate HTML, and HTML can deliver information to PHP. 1. How do I encode/decode when I pass a value with a form/url? 2. I am using <input type= "image" > tag, but $foo. x and $foo. Y variables are not available. Where are they? 3. How do I create HTML <form> arrays? 4. How do I get all the result values from a multiple-selection box?
1. How do I encode/decode when I pass a value with a form/url?

In several stages, coding is very important. Suppose you have a String Variables$data, it contains some characters that you want to pass in an encoded way, and pass the following stage encoding:

    • HTML explanation. In order to represent any string, you must enclose it in double quotes, and HTML special characters should be written using Htmlspecialchars.

    • The Url:url is made up of several parts. You want your data to be seen as part of the URL, you have to encode it with UrlEncode () .



example 52-1. Hidden HTML table cell element
<?php echo "<input type=hidden value=\" ". Htmlspecialchars ($data). "\" >\n ";? >

Note: the urlencode () $data cannot be used here because the urlencode () data is the responsibility of the browser. The vast majority of 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 is decoded to be readable because the browser interprets the HTML markup.
Once committed, either get or POST, the browser will encode the data when it is transferred, and PHP will decode it. Everything is done automatically, you don't have to do anything.

example 52-3. In the URL string
<?php echo "<a href=\" ".        Htmlspecialchars ("/nextpage.php?stage=23&data="). UrlEncode ($data)). "\" >\n ";? >

Note: you are actually writing a GET request, so it is essential to encode it with UrlEncode () .

Note: you need to Htmlspecialchars () the entire URL string, because the URL string is part of the Html-property. In this case, the browser first reverse- htmlspecialchars () the value, and then send the URL. PHP will recognize this URL string because you coded it with urlencoded () .
You will find that & is replaced in the URL string. Although you do not encode most of the browsers will also help you do, but not all of them can. So even if you're writing a static URL, you need to use htmlspecialchars () to encode the URL.


2. I am using <input type= "image" > tag, but $foo. x and $foo. Y variables are not available. Where are they?

In submitting a form, you might use an image control instead of using the standard submit button:
<input type= "image" src=http://www.163design.net/p/a/"Image.gif" Name= "foo" >
When the image control is somewhere on the user's point, 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. In other words, the dots are replaced with underscores.
3. How do I create HTML <form> arrays?

In order for your form results to be sent as an array to the PHP script, you can give the <input> <select> or <textarea> elements like the following name:
<input name= "myarray[]" ><input name= "myarray[]" ><input name= "myarray[" "><input name=" MyArray[ ] ">
Note that the brackets behind the element name cannot be saved, that is, it makes the result an array. You can arrange them into different arrays by using the element name:
<input name= "myarray[]" ><input name= "myarray[]" ><input name= "myotherarray[" "><input name=" Myotherarray[] ">
The code above generates 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]" >
The Anotherarray array will hold subscript 0, 1, email and phone.

Note: It is optional to indicate the subscript value in the element name. If you don't, the array is populated in the order that the elements appear in the form. As in our first example, the subscripts of an 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 multiple-selection box?

Multi-selection boxes in HTML are 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 is passed as a value to the processing script:
Var=option1var=option2var=option3
Each value overrides the value of the previous item. The $var solution is to use the PHP "Group of Table Cells" feature. Here are the following:
<select name= "var[]" multiple>
PHP treats $var as an array. Each selected item is assigned to an array cell. The first item is $var[0], the next item is $var [1], and so on. count ()Functions can be used to determine how many options an array has, and if necessary, you can use sort ()function to sort the array.
If you are using JavaScript, it can be an error to use the element name reference directly. You should use its digital index, or place the variable name within single quotes. For example:
variable = documents.forms[0].elements[' var[];


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.