Tutorials to .com

Tutorials to .com » Php » Others » The use of combination of PHP and HTML form to visit more than a single form value

The use of combination of PHP and HTML form to visit more than a single form value

Print View , by: iSee ,Total views: 4 ,Word Count: 2830 ,Date: Mon, 20 Apr 2009 Time: 2:01 AM

Can more easily through the HTML form for users to submit information on the ability to operate has been one of the strengths of PHP. In fact, php version 4.1 adds several visits to the new method of information and effectively remove the previous version of the most commonly used method. In this paper, the use of HTML form information submitted by the different methods and at the same time using an earlier version of PHP and the newer version. In this paper, a single value from the study started, and then can build a general access to any available page of the form value.
Note: This article assumes that you are running PHP version 3.0 or higher version of the Web server has access. You need to PHP itself, as well as to create a basic understanding of HTML forms.

HTML form
In the course of reading this article, you will see different types of HTML form elements in PHP how to provide access to information. For this example, I use a simple form of information, which the two texts from the domain, the two check boxes and a box to allow the choice of a number of components:

List 1. HTML form <html>
<head> <title> Tour Information </ title> </ head>
<body>

<h2> Mission Information </ h2>

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



In the absence of the specified method, the form using the default method of GET, browser uses to attach to it will form the value of 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 methods: visit Global Variables


2 shows a list of the code will form as a global variable value to deal with:

List 2. As a form of global variable values
<? php

echo "Ship =". $ ship;
echo "<br />";
echo "Tripdate =". $ tripdate;
echo "<br />";
echo "Exploration =". $ exploration;
echo "<br />";
echo "Contact =". $ contact;

?>



Web page generated by the values:


Ship = Midnight Runner
Tripdate = 12-15-2433
Exploration = yes
Contact =



(As you will be seen, Contact no value, because the box is not selected).

2 in the list representation is convenient, but it is only in the pseudo-PHP directive register_globals is set to on when available. In version 4.2 before, this is the default setting, many PHP developers are not even aware of the problem. However, from version 4.2 to start, register_globals default setting is off, in this case, the representation does not work, because the value is no longer appropriate to create and initialize variables.

However, you can use other methods to initialize these variables. The first is to change the value of register_globals. Many use a shared server for the developers the right to change the value of the entire server, but can change the behavior of a particular site. If you have. Htaccess file access, you can add the following pseudo-instructions enable register_globals:


php_flag register_globals on



In view of the availability of the characteristics of the uncertainty, the proposed developer is best not to use or rely on this method of access to variables. So what other options do you have?

If your system is running version 4.1 or later, your alternative is to use the import_request_variables () have a choice collection of registered global variable. You can use the import function get, post and cookie value, but if you prefer, you can add a prefix to each. 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 import value - the use of cookie value c to import - and as a result of p with the following in g, so post will cover the value of the get the value of the same name.

However, if you like, as many developers are not running version 4.1 or later, then how should we do?

Access form the value of a collection
For those running an earlier version or do not want to use global variables of people, can choose to use $ HTTP_GET_VARS and $ HTTP_POST_VARS arrays. Although not in favor of the use of these collections, but they are still available, but are still widely used. Really no longer use them, it will be added to version 4.1 of the $ _GET and $ _POST arrays to replace them.

These two types of array types are hash table (hash table). Hash table is not an integer string value to the array index. The use of form, can be passed by value to access the value of the name, such as a list of 3 below:

List 3. Through the hash table the value of access to form

<?

$ 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;
?>



Use this method, you can search through the name value of each field.

Single-name, multi-valued
Until now, each name corresponds to a value only. If there is more than the value of what will happen? For example, crew species name list box allows multiple values to submit crew.

Ideally, you want to use these values as an array so that you can retrieve them explicitly. To achieve this, you must be modified on the HTML page. Should be submitted as an array of field names in square brackets should be used, such as the crew [] in:

List 4. Modify HTML pages
...
<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, retrieve form values in fact have an array:

5 list. Will be variable as an array access
...
$ 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, after the submission of the page will show multiple values:


0) snertal
1) gosny
2)



First of all, bearing in mind that this is a subscript of the array to start from 0. The first value encountered in the position 0, the next value in the position 1, and so on. In this case, I only submitted two values, so the third is empty.

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

List 6. To determine the size of the array
...
for ($ i = 0; $ i <sizeof ($ crew_values); $ i + +) (
echo $ crew_values [$ i];
echo "<br />";
)
...



Sometimes, however, is not worth much, but there is no value.

Surprising that the disappearance of the box
Check box is selected only when the actual was presented, recognizing that this is very important. Otherwise, it will tell of the disappearance of the facts you need to know: the user does not click the check box. Use the check boxes, you can use isset () function to check whether or not explicitly set a value:

List of 7. Check whether to submit a check box
...
$ contact_value = $ HTTP_GET_VARS [ 'contact'];
echo $ contact_value;

if (isset ($ contact_value)) (
/ / The checkbox was clicked
) Else (
/ / The checkbox wasn't clicked
)
...




Access to all the form values
Check box field is expected to form the value of your name may not be completely sure of the case examples. Typically, you will find that there is a general method to form the value of access to all the routine is very useful.

Fortunately, because only $ HTTP_GET_VARS and similar hash table, you can use some of the characteristics of the array to operate them. For example, you can use array_keys () function to obtain the name of all the potential value of the list:

A list of 8. To obtain the value of the name form the list of

...
$ form_fields = array_keys ($ HTTP_GET_VARS);
for ($ i = 0; $ i <sizeof ($ form_fields); $ i + +) (

$ thisField = $ form_fields [$ i];
$ thisValue = $ HTTP_GET_VARS [$ thisField];
echo $ thisField. "=". $ thisValue;
echo "<br />";

)
...



In this case, you in fact a combination of several techniques. First of all, the search form field names and an array named $ form_fields. $ form_fields a typical array is an array, so you can use sizeof () function to determine the number of potential keys, and for each traversal cycle. For each, the name search field and then use that name to obtain the actual value. Generated Web page appears as follows:


ship = Midnight Runner
tripdate = 12-15-2433
exploration = yes
crew = Array



There are two important issues. First, contact the field there is no return value, which, as expected. Second, crew value (By the way, you may know: it is the crew rather than the name of crew []) is an array rather than value. In order to retrieve all the actual value, need to use the is_array () function to detect all the array and to deal with them:

The list 9. Dealing with an array
...
for ($ i = 0; $ i <sizeof ($ form_fields); $ i + +) (

$ thisField = $ form_fields [$ i];
$ thisValue = $ HTTP_GET_VARS [$ thisField];
if (is_array ($ thisValue)) (
for ($ j = 0; $ j <sizeof ($ thisValue); $ j + +) (
echo $ thisField. "=". $ thisValue [$ j];
echo "<br />";
)
) Else (
echo $ thisField. "=". $ thisValue;
)
echo "<br />";

)
...




The result is the actual submission of all data:


ship = Midnight Runner
tripdate = 12-15-2433
exploration = yes
crew = snertal
crew = gosny



Finally, a note: point
Now that you have to adapt to any form you submit the form value to operate the page, you will need to pump a little more time to consider a PHP programmer often surprised by the situation.

In some cases, designers have chosen to use a graphical button instead of submit button, graphics button as shown in Figure 2, the code as shown in the list of 10.

A list of 10. Add graphics button
...
<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="image" src="button.gif" name="formbutton"/>
</ td>
</ tr>
</ table>
...



Figure 2. Form of graphics button




Please note, although the figure above that there is only one image, there are two graphics button (or the desired results). As a developer, you can inspect and value along with the return of the x and y coordinates to know where the user clicked. In fact, as is likely to submit the form to create at the end of the URL shown below and the query string:


... snertal & crew% 5B% 5D = gosny & formbutton.x = 37 & formbutton.y = 14


Please note that the name attached to the button. X and. Y. However, if you plan to submit the page and then view the results, you'll see:


ship = Midnight Runner
tripdate = 12-15-2433
exploration = yes
crew = snertal
crew = gosny

formbutton_x = 37
formbutton_y = 14



Please note that the dots (.) Have been converted to underscore (_). It seems remarkable, but it is necessary, because the variable names in PHP can not be a bit number, $ formbutton.x therefore is an illegal variable name. In fact, the name of any form No. point - not only for the image button - have been converted to underscores.

Concluding remarks
In this article, you see the user through the HTML or XHTML form submission information visit several ways. How to deal with this information depends on the version you are using PHP and you can form variables as global variables for a visit. In all cases, the form can be used as an array of values to use, and you can array cycle traverse characteristics of all available values.


php other Articles


Can't Find What You're Looking For?


Rating: 12345.0

Comments

No comments posted.