Table of Contents
Mission Information
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

Jul 13, 2016 pm 05:28 PM
html php use value and Multiple right submit user of combine form access pass

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

React's Role in HTML: Enhancing User Experience React's Role in HTML: Enhancing User Experience Apr 09, 2025 am 12:11 AM

React combines JSX and HTML to improve user experience. 1) JSX embeds HTML to make development more intuitive. 2) The virtual DOM mechanism optimizes performance and reduces DOM operations. 3) Component-based management UI to improve maintainability. 4) State management and event processing enhance interactivity.

See all articles