WEB development in Linux: using HTML forms with PHP

Source: Internet
Author: User
Article title: WEB development in Linux: using HTML forms with PHP. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
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.
  
   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
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.