Use HTML forms in combination with PHP to access Single and Multiple form values

Source: Internet
Author: User
The ability to easily operate information submitted by users through HTML forms has always been one of the advantages of PHP. In fact, PHP version 4.1 adds several new methods to access this information and effectively removes the most commonly used methods in previous versions. This article studies different SyntaxHighlighter. all ();

The ability to easily operate information submitted by users through HTML forms has always been one of the advantages of PHP. In fact, PHP version 4.1 adds several new methods to access this information and effectively removes the most commonly used methods in previous versions. This article studies different methods of submitting information using HTML forms, and uses earlier PHP versions and newer versions. This article begins with studying a single value, and then builds a page that can access any available form value in general. Note: This document assumes that you have access to Web servers running PHP version 3.0 or later. You need to have a basic understanding of PHP and HTML forms. When reading this article, you will see how HTML form elements of different types provide PHP accessible information. For this example, I used a simple information form consisting of two text fields, two check boxes, and one option box that allows multiple numbers: list 1. HTML form Tour Information Mission Information The form uses the default GET method without specifying a method, and the browser uses it to append the form value to the URL, as shown below: Ship = Midnight + Runner & tripdate = 12-15-2433 & Authentication = yes & crew = snertal & crew = gosny Figure 1 shows the form itself. Figure 1. HTML form old method: Access the code shown in global variable list 2 to process the form value as a global variable: list 2. form value as a global variable "; Echo" Tripdate = ". $ tripdate; echo"
"; Echo" annotation = ". $ annotation; echo"
"; Echo" Contact = ". $ contact;?> The generated Web page shows the submitted value: Ship = Midnight Runner Tripdate = 12-15-2433 conflict = yes Contact = (as you will see later, there is no Contact value, because no box is selected ). The representation in listing 2 is of course convenient, but it is only available when the PHP pseudo command register_globals is set to on. Before version 4.2, this was the default setting, and many PHP developers did not even realize this problem. However, since version 4.2, the default setting of register_globals is off. in this case, this notation does not work properly because it is no longer used to create and initialize variables with appropriate values. However, you can use other methods to initialize these variables. The first method is to change the value of register_globals. Many developers who use shared servers do not have the right to change the value for the entire server, but can change the behavior for a specific site. If you have. to access the htaccess file, you can add the following pseudo command to enable register_globals: php_flag register_globals on. Given the uncertainty about the availability of this feature, it is recommended that developers do not use or rely on this method to obtain variables. So what options do you have? If your system is running version 4.1 or later, another option is to use import_request_variables () to selectively register a global variable set. You can use this function to import get, post, and cookie values. you can also add a prefix for each item if you want. For example: "; Echo" Tripdate = ". $ formval_tripdate; echo"
"; Echo" comment = ". $ formval_comment ation; echo"
"; Echo" Contact = ". $ formval_contact;?> Here, get and post values are imported-use c to import cookie values-and because p is behind g, the post value will overwrite the get value of the same name. But what if you do not run version 4.1 or later like many developers? For those who run earlier versions or do not want to use global variables, you can use the $ HTTP_GET_VARS and $ HTTP_POST_VARS arrays. Although they are not in favor of using these sets, they are still available and widely used. When they are no longer used, they will be replaced by the $ _ GET and $ _ POST arrays added in version 4.1. The two types of arrays are hash tables ). A hash is an array that creates an index by using a string value instead of an integer. When using a form, you can access the value by using the value name, as shown in listing 3: Listing 3. accessing the form value through the hash list "; $ Tripdate_value = $ HTTP_GET_VARS [tripdate]; echo $ tripdate_value; echo"
"; $ Configuration_value = $ HTTP_GET_VARS [configuration]; echo $ configuration_value; echo"
"; $ Contact_value = $ HTTP_GET_VARS [contact]; echo $ contact_value;?> By using this method, you can retrieve the values of each field by name. Single name, multi-value until now, each name corresponds to only one value. What if there are multiple values? For example, the crew species list box allows multiple values to be submitted with the name crew. Ideally, you want to use these values as arrays so that they can be retrieved explicitly. To achieve this, you must slightly modify the HTML page. The fields to be submitted as arrays should be named in square brackets, such as listing 4 in crew []. modify the HTML page... Xebrax Snertal Gosny ... Once you make the changes, retrieving the form value actually generates an array: list 5. access variables as arrays... $ crew_values = $ HTTP_GET_VARS [crew]; echo "0 )". $ crew_values [0]; echo"
"; Echo" 1) ". $ crew_values [1]; echo"
"; Echo" 2 )". $ crew_values [2];... now, after the page is submitted, multiple values are displayed: 0) snertal 1) gosny 2) First, note that this is an array whose subscript starts from 0. The first encountered value is in position 0, the next value is in position 1, and so on. In this example, I submitted only two values, so the third item is empty. Generally, you do not know how many items will be submitted, so you can use the fact that it is an array to use the sizeof () function to determine the number of submitted values without directly calling each item: listing 6. determine the size of the array... for ($ I = 0; $ I <sizeof ($ crew_values); $ I ++) {echo $ crew_values [$ I]; echo"
";}... However, sometimes the problem is not that there are too many values, but that there is no value at all. This is important to recognize that the checkbox that is surprisingly missing is submitted only when it is selected. Otherwise, its disappearance will tell you the truth you need to know: the user does not click the check box. When using the check box, you can use the isset () function to explicitly check whether a value is set: list 7. check whether the checkbox is submitted... $ contact_value = $ HTTP_GET_VARS [contact]; echo $ contact_value; if (isset ($ contact_value )) {// The checkbox was clicked} else {// The checkbox wasnt clicked }... the checkbox field for obtaining all form values is just one example of a situation where you may not be completely confident about the expected form value name. Generally, you will find a common method to access all form values is very useful. Fortunately, because $ HTTP_GET_VARS and its similar classes are only scattered lists, you can use some features of arrays to operate on them. For example, you can use the array_keys () function to obtain a list of all potential value names: listing 8. get the list of form value names... $ form_fields = array_keys ($ HTTP_GET_VARS); for ($ I = 0; $ I <sizeof ($ form_fields); $ I ++) {$ thisField = $ form_fields [$ I]; $ thisValue = $ HTTP_GET_VARS [$ thisField]; echo $ thisField. "= ". $ thisValue; echo"
";}... In this example, you actually combine several technologies. First, retrieve the array of form field names and name it $ form_fields. The $ form_fields array is a typical array. Therefore, you can use the sizeof () function to determine the number of potential keys and traverse each item cyclically. For each item, retrieve the field name and use the name to obtain the actual value. The generated Web page looks as follows: ship = Midnight Runner tripdate = 12-15-2433 annotation = yes crew = Array. There are two important items here. First, the contact field has no return value at all, as expected. Second, the crew value (by the way, you may know that its name is crew rather than crew []) is an array rather than a value. To actually retrieve all values, use the is_array () function to detect all arrays and process them accordingly: listing 9. processing arrays... for ($ I = 0; $ I <sizeof ($ form_fields); $ I ++) {$ thisField = $ form_fields [$ I]; $ thisValue = $ HTTP_GET_VARS [$ thisField]; if (is_array ($ thisValue) {for ($ j = 0; $ j <sizeof ($ thisValue); $ j ++) {echo $ thisField. "= ". $ thisValue [$ j]; echo"
";}} Else {echo $ thisField." = ". $ thisValue;} echo"
";}... The result is the last description of all actually submitted data: ship = Midnight Runner tripdate = 12-15-2433 authentication = yes crew = snertal crew = gosny: now that you have a form operation page that is suitable for submitting any form value, you need to take some time to consider a situation that often surprises PHP programmers. In some cases, the designer selects a graphic button instead of a submit button, as shown in Figure 2, and the code is shown in listing 10. Listing 10. add a graphic button... Crew sp

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: 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.