Combine PHP with HTML forms to access single and multiple form values _php tutorial

Source: Internet
Author: User
The ability to easily manipulate the information that users submit via HTML forms has been one of the advantages of PHP. In fact, PHP version 4.1 adds several new ways to access this information and effectively removes one of the most commonly used methods from previous releases. This article explores different ways to use the information submitted on an HTML form and uses both earlier versions of PHP and newer versions. This article begins with a study of a single value, and then constructs a page that provides general access to any available form values. Note: This article assumes that you have access to a WEB server running PHP version 3.0 or later. You need to have a basic understanding of PHP itself and the creation of HTML forms. HTML form as you read this article, you'll see how different types of HTML form elements provide information that PHP can access. For this example, I used a simple information form that consists of two text fields, two check boxes, and a selection box that allows multiple items: Listing 1. HTML form <title>Tour information</title>

Mission Information

Without specifying a method, the form uses the default method GET, which the browser uses to append the form value to the URL, as follows: Http://www.vanguardreport.com/formaction.php? Ship=midnight+runner&tripdate=12-15-2433&exploration=yes&crew=snertal&crew=gosny Figure 1 shows the form itself. Figure 1. HTML form the old way: Access the code shown in the global variables Listing 2 takes the form value as a global variable: Listing 2. Form values as global variables "; echo "tripdate =". $tripdate; echo "
"; echo "exploration =". $exploration; echo "
"; echo "contact =". $contact;?> generated Web page displays the submitted value: Ship = Midnight Runner tripdate = 12-15-2433 Exploration = yes contact = (as you will see later, the contact has no value because the box is not selected). The notation in Listing 2 is, of course, convenient, but it is only available when the PHP pseudo-directive register_globals is set to ON. Before version 4.2, this was the default setting, and many PHP developers didn't even realize that there was a problem. However, starting with version 4.2, the default setting for Register_globals is off, in which case the notation does not work properly because the variable is no longer created and initialized with the appropriate values. However, you can initialize these variables in other ways. The first method is to change the value of a register_globals. Many developers who use shared servers do not have permission to change this value for the entire server, but can change behavior for a specific site. If you have access to a. htaccess file, you can enable Register_globals:php_flag register_globals on given the uncertainty of whether the feature is available by adding the following pseudo-directive, It is advisable for developers not to use or rely on this method of acquiring variables. So what are your options? If your system is running version 4.1 or later, your other option is to register the global variables collection selectively with Import_request_variables (). You can use this function to import get, post, and cookie values, and you can add a prefix to each item if you wish. For example: "; echo "tripdate =". $formval _tripdate; echo "
"; echo "exploration =". $formval _exploration; echo "
"; echo "contact =". $formval _contact;?> here, the get and post values are imported-using C to import the cookie value-and since p follows G, the post value overwrites the Get value with the same name. But what if you don't run version 4.1 or later like many developers? Access Form value collection for those who run an earlier version or are reluctant to use global variables, you can choose to use the $HTTP _get_vars and $HTTP _post_vars arrays. Although the use of these collections is not favoured, they are still available and are still widely used. When they are no longer used, they are replaced with the $_get and $_post arrays added in version 4.1. Both types of arrays are hash lists (hash table). A hash table is an array that is indexed by string values rather than integers. When using a form, you can access the value by its name, as shown in Listing 3: Listing 3. Accessing form values from a hash table "; $tripdate _value = $HTTP _get_vars[tripdate]; echo $tripdate _value; echo "
"; $exploration _value= $HTTP _get_vars[exploration]; echo $exploration _value; echo "
"; $contact _value = $HTTP _get_vars[contact]; echo $contact _value;?> using this method, you can retrieve the value of each field by its name. Sole name, multiple values so far, each name corresponds to only one value. What happens if there are multiple values? For example, the Crew species list box allows multiple values to be submitted with the name crew. Ideally, you would want to use these values as arrays so that you can retrieve them explicitly. To do this, you must make a slight change to the HTML page. The field to be submitted as an array should be named in square brackets, such as crew[]: Listing 4. Modify HTML Page ... XebraxSnertalGosny... Once you make changes, retrieving the form values actually produces an array: Listing 5. Access the variable as an array ... $crew _values = $HTTP _get_vars[crew]; echo "0)". $crew _values[0]; echo "
"; echo "1)". $crew _values[1]; echo "
"; echo "2)". $crew _values[2]; ... Multiple values are now displayed after the page is submitted: 0) Snertal 1) Gosny 2) First notice that this is an array of subscripts starting at 0. The first encountered value is in position 0, the next value is at position 1, and so on. In this case, I only submitted two values, so the third item is empty. Typically, you don't know how many items will be committed, so you can use the sizeof () function to determine how many values are committed without having to call each entry directly, using the fact that it is an array: 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 too many values, but there is no value at all. It is important to recognize that the amazing disappearing check box is only submitted when it is actually selected. Otherwise, it disappears to tell you what you need to know: The user does not click the check box. When you use a check box, you can use the Isset () function to explicitly check whether a value is set: Listing 7. Check if 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 wasnt clicked} ... The Get all Form values check box field is just one example of a scenario where you might not be completely sure about the expected form value name. In general, you will find it useful to have a routine that accesses all of the form values in a common way. Fortunately, because $HTTP _get_vars and its peers are just hash lists, you can manipulate them with some of the attributes of the array. For example, you can use the Array_keys () function to get a list of all potential value names: Listing 8. Get a 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 case, you are actually combining several techniques. First, retrieve an array of form field names and name them $form _fields. The $form _fields array is just a typical array, so you can use the sizeof () function to determine the number of potential keys and iterate through each item. For each item, retrieve the name of the field and use that name to get the actual value. The resulting Web page looks like this: Ship = Midnight Runner tripdate = 12-15-2433 Exploration = Yes crew = Array There are two important things here. First, the Contact field does not have a return value at all, as expected. Second, the crew value (by the way, you might know: its name is crew instead of crew[]) is an array instead of a value. In order to actually retrieve all the values, you need to use the Is_array () function to detect all the arrays and process them accordingly: Listing 9. Handle array ... for ($i = 0; $i < sizeof ($form _fields); $i + +) {$thisField = $form _fields[$i]; $thisValue = $HTTP _get_vars[$th Isfield]; 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 data that has actually been committed: Ship = Midnight Runner tripdate = 12-15-2433 Exploration = Yes crew = Snertal crew = Gosny Last Note: Point now that you have a suitable When you submit a Form Action page for any form value, you need to take a moment to consider a scenario that often surprises PHP programmers. In some cases, the designer chooses to use a graphical button instead of a Submit button, as shown in Figure 2, and the code is shown in Listing 10. Listing 10. Add Graphics button ... Crew SP

http://www.bkjia.com/PHPjc/531723.html www.bkjia.com true http://www.bkjia.com/PHPjc/531723.html techarticle The ability to easily manipulate the information that users submit via HTML forms has been one of the advantages of PHP. In fact, PHP version 4.1 adds several new ways to access this information and ...

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