Home > Backend Development > PHP Tutorial > Use HTML forms with PHP to access single and multiple form values_PHP tutorial

Use HTML forms with PHP to access single and multiple form values_PHP tutorial

WBOY
Release: 2016-07-13 17:28:57
Original
977 people have browsed it

The ability to more easily operate on information submitted by users through HTML forms has always been one of PHP's strengths. In fact, PHP version 4.1 adds several new methods for accessing this information and effectively removes one of the most commonly used methods from previous versions. This article examines different ways of working with information submitted on an HTML form, using both older and newer versions of PHP. This article starts by studying a single value and then builds a page that can generally access any available form value. Note: This article assumes you have access to a web server running PHP version 3.0 or higher. You need a basic understanding of PHP itself and creating HTML forms. HTML Forms 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 consisting of two text fields, two checkboxes, and a select box that allows multiple items: Listing 1. HTML form

Tour Information

Mission Information

In the absence of a specified method, the form uses the default method GET, which is used by the browser to append the form value to the URL. As shown below: 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 forms the old way: accessing global variables The code shown in Listing 2 handles form values ​​as global variables: Listing 2. Form values ​​as global variables "; echo "Tripdate = ".$tripdate; echo "
"; echo "Exploration = ".$exploration; echo "
"; echo "Contact = ".$contact; ?> The generated Web page displays the submitted value: Ship = Midnight Runner Tripdate = 12-15 -2433 Exploration = yes Contact = (As you will see later, Contact has no value because the box is not checked) The notation in Listing 2 is certainly convenient, but it is only set if the PHP directive register_globals. Available only when on. Before version 4.2, this was the default setting, and many PHP developers were not even aware of this problem. However, starting with version 4.2, the default setting for register_globals is off, in which case. This notation does not work properly because the variables are 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 register_globals. Many developers using shared servers do not have the permission. Change this 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 directive: php_flag register_globals on Given the uncertainty about whether this feature is available. , developers are advised not to use or rely on this method of obtaining variables. So what are your options? If your system is running version 4.1 or higher, your other option is to use import_request_variables(). Optionally register a collection of global variables. You can use this function to import get, post, and cookie values, and prefix 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 will overwrite the get value of the same name. But what if you, like many developers, are not running version 4.1 or higher? Accessing a Collection of Form Values ​​For those running older versions or who prefer not to use global variables, there is an option to use the $HTTP_GET_VARS and $HTTP_POST_VARS arrays. Although these collections are deprecated, they are still available and are still widely used. When they are no longer used, they will be replaced by the $_GET and $_POST arrays added in version 4.1. The types of these two types of arrays are hash tables. A hash table is an array indexed by string values ​​rather than integers. When working with forms, you can access values ​​by their names, as shown in Listing 3: Listing 3. Accessing form values ​​via hash table $ship_value = $HTTP_GET_VARS[ship]; echo $ship_value; echo " $ship_value = $HTTP_GET_VARS[ship]; echo $ship_value; echo "
"; $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 name. Single name, multiple values ​​Until now, each name corresponded to only one value. What happens if there are multiple values? For example, the crew species listbox allows multiple values ​​to be submitted with the name crew. Ideally, you want to use the values ​​as an array so you can retrieve them explicitly. You must modify the HTML page slightly. Fields to be submitted as arrays should be named with square brackets, as in crew[]: Listing 4. Modify the HTML page... ... Once you make the change, retrieving the form values ​​actually results in an array: Listing 5. Accessing variables 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]; . .. Now, after submitting the page, multiple values ​​will be displayed: 0) snertal 1) gosny 2) First notice that this is an array with indexes starting from 0. The first encountered value is in position 0, and the following ones. The value is at position 1, and so on. In this case, I only submitted two values, so the third item is empty. Usually, you don't know how many items will be submitted, so you can take advantage of the fact that it is an array. sizeof() function to determine how many values ​​were submitted without having to call each item directly: Listing 6. Determining the size of the array... for ($i = 0; $i "; } ... However, sometimes the problem isn't too many values, but rather no values ​​at all. The surprisingly disappearing checkbox only appears when it's actually selected. It's important to realize that otherwise, its disappearance tells you all you need to know: the user didn't click the checkbox. When using a checkbox, you can check it explicitly using the isset() function. Whether the value was set: Listing 7. Check if the checkbox was submitted... $contact_value = $HTTP_GET_VARS[contact]; echo $contact_value; if (isset($contact_value)) { //The checkbox was clicked } else { / /The checkbox wasn't clicked } ... Getting all form values ​​The checkbox field is just one example of a situation where you might not be completely sure about the expected form value names. Often, you will find it useful to have a routine that accesses all form values ​​in a common way. Fortunately, because $HTTP_GET_VARS and its ilk are just hash tables, you can manipulate them with some properties of arrays. 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 "; } .. . In this example, you're actually combining several techniques. First, retrieve an array of form field names and name it $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 loop over each item. For each item, retrieve the name of the field and then 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 returns no value at all, as expected. Second, the crew value (which, by the way, you probably know: its name is crew, not crew[]) is an array, not a value. In order to actually retrieve all values, you need to detect all arrays using the is_array() function and process them accordingly: Listing 9. Processing arrays... for ($i = 0; $i "; } } else { echo $thisField ." = ". $thisValue; } echo "
"; } ... The result is all Actual data submitted: ship = Midnight Runner tripdate = 12-15-2433 exploration = yes crew = snertal crew = gosny One final note: Now that you have a form action page that can accommodate any form value you submit, you need Take a moment to consider a situation that often surprises PHP programmers. In some cases, designers choose 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. Adding a graphic button ... Crew sp

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/531723.htmlTechArticleThe ability to easily operate on information submitted by users through HTML forms has always been one of PHP's strengths. In fact, PHP version 4.1 adds several new ways to access this information and...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template