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 describes how to use HTML forms to submit information, using earlier PHP versions and newer versions, it is always one of the advantages of PHP to easily operate the information submitted by users through HTML forms. 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.

HTML form
When reading this article, you will see how different types of HTML form elements 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:

Listing 1. HTML form
Tour Information


Mission Information








If no method is specified, the form uses the default GET method, and the browser uses it to append the form value to the URL, as shown below:


Http://www.vanguardreport.com/formaction.php?
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 global variables


The code shown in listing 2 treats the form value as a global variable:

Listing 2. form values as global variables

Echo "Ship =". $ ship;
Echo"
";
Echo "Tripdate =". $ tripdate;
Echo"
";
Echo "annotation =". $ annotation;
Echo"
";
Echo "Contact =". $ contact;

?>



The submitted value is displayed on the generated Web page:


Ship = Midnight Runner
Tripdate = 12-15-2433
Token = yes
Contact =



(As you will see later, there is no Contact value because the box is not 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 access to the. htaccess file, you can enable register_globals by adding the following pseudo command:


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:



Import_request_variables (gp, "formval _");

Echo "Ship =". $ formval_ship;
Echo"
";
Echo "Tripdate =". $ formval_tripdate;
Echo"
";
Echo "comment ation =". $ 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?

Set of Access form values
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 use the value name to access the value, as shown in listing 3:

Listing 3. accessing form values through a hash


$ Ship_value = $ HTTP_GET_VARS ['ship '];
Echo $ ship_value;
Echo"
";

$ 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
So far, each name has 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. Fields to be submitted as arrays should be named in square brackets, such as in crew:

Listing 4. modifying the HTML page
...

Xebrax Snertal Gosny

...



Once you make the changes, retrieving the form value actually generates an array:

Listing 5. accessing 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, multiple values are displayed after the page is submitted:


0) snertal
1) gosny
2)



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 array size
...
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.

Surprisingly disappearing check box
The check box is submitted only when it is selected. this is important. 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:

Listing 7. check whether the check box is submitted
...
$ Contact_value = $ HTTP_GET_VARS ['contact'];
Echo $ contact_value;

If (isset ($ contact_value )){
// The checkbox was clicked
} Else {
// The checkbox wasn' t clicked
}
...




Get all form values
The check box field 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. retrieving 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
Token = yes
Crew = Array



There are two important issues 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. handling 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 all submitted data:


Ship = Midnight Runner
Tripdate = 12-15-2433
Token = yes
Crew = snertal
Crew = gosny



Last Note: Point
Now that you have a form operation page that is suitable for submitting any form value, you need to take a moment 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 species:

Xebrax Snertal Gosny








...



Figure 2. graphical buttons on the form




Note that although there is only one image, there are two graphical buttons (or expected results ). As a developer, you can check the x and y coordinates returned together with the value to know where the user clicked. In fact, submitting the form as is may create a URL and a query string ending with the following:


... Snertal & crew % 5B % 5D = gosny & formbutton. x = 37 & formbutton. y = 14


Note that. x and. y are appended to the button name. However, if you want to submit the page and view the result, you will see:


Ship = Midnight Runner
Tripdate = 12-15-2433
Token = yes
Crew = snertal
Crew = gosny

Formbutton_x = 37
Formbutton_y = 14



Note that the period (.) has been converted to an underscore (_). This seems a bit strange, but it is necessary because the variable name in PHP cannot have a number, so $ formbutton. x is an invalid variable name. In fact, any dot in the form name-not just for image buttons-is converted into underscores.

Conclusion
In this article, you can see several methods to access information submitted by users through HTML or XHTML forms. How to handle this information depends on the PHP version you are using and whether you can use Form variables as global variables for access. In any case, the form value can be used as an array, and you can use the array feature to traverse all available values cyclically.
Related Article

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.