Using HTML forms with PHP

Source: Internet
Author: User
Tags array arrays hash html form html page variables php and versions
The ability to easily manipulate the information that users submit through HTML forms has been one of the advantages of PHP. In fact, PHP version 4.1 adds several new ways to access the information and effectively removes one of the most commonly used methods in previous versions. This article studied different ways to use the information submitted on an HTML form and used both earlier versions of PHP and newer versions. This article starts with a study of a single value and then builds a page that can generally access 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 will 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



<body>


<form action= "formaction.php" >

<table width= "100%" >

<tr><td>ship name:</td><td><input type= "text" name= "ship"/></td></tr>

<tr><td>trip date:</td><td><input type= "text" name= "Tripdate"/></td></tr >

&LT;TR&GT;&LT;TD colspan= "2" >mission goals:</td></tr>

<tr>

<td><input type= "checkbox" Name= "Exploration" value= "yes"/>

Exploration</td>

<td><input type= "checkbox" name= "Contact" value= "yes"/>

Contact</td>

</tr>

<tr>

&LT;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>

&LT;TR&GT;&LT;TD colspan= "2" align= "center" ><input type= "Submit"/></td></tr>

</table>

</form>

</body>


In the absence of a method specified, the form uses the default method get, which the browser uses to attach 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 fashioned: accessing global variables

The code shown in Listing 2 handles the form value as a global variable:

Listing 2. Form values as global variables

<?php

echo "ship =". $ship;

echo "<br/>";

echo "tripdate =". $tripdate;

echo "<br/>";

echo "exploration =". $exploration;

echo "<br/>";

echo "contact =". $contact;

?>

The generated Web page displays the submitted value:

ship = Midnight Runner

Tripdate = 12-15-2433

Exploration = yes

Contact =

(as you'll see later, the contact has no value because the box is not selected).

The notation in Listing 2 is convenient, of course, but it is only available if the PHP pseudo instruction Register_globals is set to ON. This is the default setting before version 4.2, and many PHP developers are not even aware of the problem. However, starting with version 4.2, the default setting for Register_globals is off, in which case the notation does not work 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 the register_globals. Many developers who use shared servers do not have the right to change the value for the entire server, but can change behavior for a particular site. If you have access to the. htaccess file, you can enable register_globals by adding the following pseudo directive:

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 of obtaining variables. So what are your options?

If your system is running version 4.1 or later, your other option is to use Import_request_variables () to selectively register the global variables collection. You can use this function to import get, post, and cookie values, and you can prefix each item if you wish. For example:

<?php

Import_request_variables (GP, "Formval_");

echo "ship =". $formval_ship;

echo "<br/>";

echo "tripdate =". $formval_tripdate;

echo "<br/>";

echo "exploration =". $formval_exploration;

echo "<br/>";

echo "contact =". $formval_contact;

?>

Here, the get and post values are imported-the cookie value is imported using C-and since p is followed by 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?

accessing form Values Collection

For those who run older versions or do not want 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 you seriously no longer use them, you replace them with the $_get and $_post arrays added in version 4.1.

Both types of arrays are hashed (hash table). A hash list is an array that is indexed by a string value instead of an integer. When working with a form, you can access the value by the name of the value, as shown in Listing 3:

Listing 3. Accessing form values through a hash list

?

$ship_value = $http_get_vars[' ship '];

Echo $ship_value;

echo "<br/>";

$tripdate_value = $http_get_vars[' tripdate '];

Echo $tripdate_value;

echo "<br/>";

$exploration_value= $http_get_vars[' exploration '];

Echo $exploration_value;

echo "<br/>";

$contact_value = $http_get_vars[' contact '];

Echo $contact_value;

?>

Using this method, you can retrieve the value of each field by name.

Single-name, multi-valued
Until now, each name corresponds to only one value. What happens if you have more than one value? For example, the Crew species list box allows multiple values to be committed with the name crew.

Ideally, you 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 fields to be submitted as an array should be named in square brackets, such as crew[]:

Listing 4. Modify HTML Page

...

<td>

<select name= "crew[]" multiple= "multiple" >

<option value= "Xebrax" >Xebrax</option>

<option value= "Snertal" >Snertal</option>

<option value= "Gosny" >Gosny</option>

</select>

</td>

...
Once you make a change, retrieving the form values actually produces an array:


 

Listing 5. To access a variable as an array

...

$crew_values = $http_get_vars[' crew '];

echo "0)". $crew_values[0];

echo "<br/>";

echo "1)". $crew_values[1];

echo "<br/>";

echo "2)". $crew_values[2];

...

Now, multiple values are displayed after the page is submitted:

0) Snertal

1) Gosny

2)
The first thing to note is that this is an array of subscripts starting with 0. The first encountered value is in position 0, the next value is at position 1, and so on. In this case, I submit only two values, so the third item is empty.

Typically, you do not know how many items will be committed, so you can take advantage of the fact that it is an array using the sizeof () function to determine how many values are committed without having to call each item directly:

Listing 6. Determining the size of an array

...

for ($i = 0; $i < sizeof ($crew_values); $i++) {

Echo $crew_values[$i];

echo "<br/>";

}

...
However, sometimes the problem is not too much, but there is no

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.