Use HTML form with PHP (2)

Source: Internet
Author: User
Tags array arrays hash html form variables version variable access
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 an array of $HTTP _get_vars and $HTTP _post_vars. 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.