PHP uses forms 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 (as the mainstream development language. In fact, php (as the mainstream development language) 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 on HTML forms, and uses earlier versions and newer versions of php (as the mainstream development language. 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 (as the mainstream development language) version 3.0 or later. You need to have a basic understanding of php (as the mainstream development language) and HTML form creation.
  
  HTML form
When reading this article, you will see how different types of HTML form elements provide php (as the mainstream development language) access 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 <Head> <title> Tour Information </title> <Body>
  
<H2> Mission Information   
<Form action = "formaction. php (as the mainstream development language)">
& Lt; table width = "100%" & gt;
<Tr> <td> Ship Name: </td> <input type = "text" name = "ship"/> </td> </tr>
<Tr> <td> Trip Date: </td> <input type = "text" name = "tripdate"/> </td> </tr>
<Tr> <td colspan = "2"> Mission Goals: </td> </tr>
<Tr>
<Td> <input type = "checkbox" name = "comment ation" value = "yes"/>
Token </td>
<Td> <input type = "checkbox" name = "contact" value = "yes"/>
Contact </td>
</Tr>
<Tr>
<Td valign = "top"> Crew species: </td>
<Td>
<Select name = "crew" multiple = "multiple">
<Option value = "xebrax"> Xebrax </option>
<Option value = "snertal"> Snertal </option>
<Option value = "gosny"> Gosny </option>
</Select>
</Td>
</Tr>
<Tr> <td colspan = "2" align = "center"> <input type = "submit"/> </td> </tr>
</Table>
</Form>
  
</Body>
</Html>
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 (as the mainstream development language )?
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
<? Php (as the mainstream development language)
  
Echo "Ship =". $ ship;
Echo "<br/> ";
Echo "Tripdate =". $ tripdate;
Echo "<br/> ";
Echo "annotation =". $ annotation;
Echo "<br/> ";
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 notation in Listing 2 is of course convenient, but it is only available when php (as the mainstream development language) pseudo command register_globals is set to on. Before Version 4.2, this was the default setting. Many php (as the current mainstream development language) 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.

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.